Skip to content

Commit

Permalink
added configuration to enable/disable verbose error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jijisv committed Mar 13, 2018
1 parent 9bfa716 commit cf8c75e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 51 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.91.10
version=0.91.11
springVersion=4.3.3.RELEASE
springBootVersion=1.4.1.RELEASE
jerseyVersion=2.24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.aol.micro.server.general.exception.mapper;

import java.util.HashSet;
import java.util.Set;


import com.aol.micro.server.Plugin;
import cyclops.collections.immutable.PersistentSetX;

Expand All @@ -28,10 +24,5 @@ public PersistentSetX<String> jaxRsPackages() {
public PersistentSetX<Class> springClasses() {
return PersistentSetX.of(MapOfExceptionsToErrorCodes.class);
}






}
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
package com.aol.micro.server.general.exception.mapper;


import static org.jooq.lambda.tuple.Tuple.tuple;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import org.jooq.lambda.tuple.Tuple2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.jooq.lambda.tuple.Tuple;
import org.jooq.lambda.tuple.Tuple2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.jooq.lambda.tuple.Tuple.tuple;

@Service
@Provider
public class GeneralExceptionMapper implements ExceptionMapper<Exception> {

final Logger logger;

GeneralExceptionMapper(Logger logger){
this.logger = logger;
}

public GeneralExceptionMapper(){
this.logger = LoggerFactory.getLogger(GeneralExceptionMapper.class);;
}
private final boolean showDetails;

GeneralExceptionMapper(Logger logger, boolean showDetails){
this.logger = logger;
this.showDetails = showDetails;
}

@Autowired
public GeneralExceptionMapper(@Value("${micro.general.exception.mapper.details:true}") Boolean showDetails){
this(LoggerFactory.getLogger(GeneralExceptionMapper.class), showDetails);
}


Map<Class<? extends Exception>, Tuple2<String, Status>> mapOfExceptionsToErrorCodes = MapOfExceptionsToErrorCodes.getMergedMappings();
Expand All @@ -48,25 +53,30 @@ public Response toResponse(final Exception ex) {

final String errorTrackingId = UUID.randomUUID().toString();

Tuple2<String, Status> error = new Tuple2<String, Status>(MapOfExceptionsToErrorCodes.INTERNAL_SERVER_ERROR, Status.INTERNAL_SERVER_ERROR);
Tuple2<String, Status> error = new Tuple2<>(MapOfExceptionsToErrorCodes.INTERNAL_SERVER_ERROR, Status.INTERNAL_SERVER_ERROR);

Optional<Tuple2<String, Status>> errorFromLookup = find(ex.getClass());

if (errorFromLookup.isPresent()) {
error = errorFromLookup.get();

} else {
if(ex instanceof javax.ws.rs.WebApplicationException){
javax.ws.rs.WebApplicationException rsEx = ((javax.ws.rs.WebApplicationException)ex);
error = tuple(rsEx.getResponse().getStatusInfo().getReasonPhrase(),Status.fromStatusCode(rsEx.getResponse().getStatus()));
if (ex instanceof javax.ws.rs.WebApplicationException) {
javax.ws.rs.WebApplicationException rsEx = ((javax.ws.rs.WebApplicationException) ex);
error = tuple(rsEx.getResponse().getStatusInfo().getReasonPhrase(), Status.fromStatusCode(rsEx.getResponse().getStatus()));

}
logger.error( String.format("%s Error id: %s", error.v1(), errorTrackingId) + ex.getMessage(), ex);
}
logger.warn( String.format("%s Error id: %s", error.v1(), errorTrackingId));
logger.error(String.format("%s Error id: %s, %s", error.v1(), errorTrackingId, ex.getMessage()), ex);

Response.ResponseBuilder responseBuilder = Response.status(error.v2()).type(MediaType.APPLICATION_JSON_TYPE);

if (showDetails) {
responseBuilder.entity(new ExceptionWrapper(error.v1(), String.format("Error id: %s %s", errorTrackingId, ex.getMessage())));
} else {
responseBuilder.entity(new ExceptionWrapper(MapOfExceptionsToErrorCodes.INTERNAL_SERVER_ERROR, errorTrackingId));
}

return Response.status(error.v2())
.entity(new ExceptionWrapper(error.v1(), String.format("Error id: %s %s", errorTrackingId, ex.getMessage())))
.type(MediaType.APPLICATION_JSON_TYPE).build();
return responseBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class MapOfExceptionsToErrorCodes {

private static LinkedHashMap<Class<? extends Exception>, Tuple2<String, Status>> createMap(){
LinkedHashMap<Class<? extends Exception>, Tuple2<String, Status>> mapOfExceptionsToErrorCodes = new LinkedHashMap<>();
mapOfExceptionsToErrorCodes.put(EOFException.class, new Tuple2<String, Status>(EMPTY_REQUEST, Status.BAD_REQUEST));
mapOfExceptionsToErrorCodes.put(JsonProcessingException.class, new Tuple2<String, Status>(JSON_PROCESSING_EXCEPTION, Status.BAD_REQUEST));
mapOfExceptionsToErrorCodes.put(EOFException.class, new Tuple2<>(EMPTY_REQUEST, Status.BAD_REQUEST));
mapOfExceptionsToErrorCodes.put(JsonProcessingException.class, new Tuple2<>(JSON_PROCESSING_EXCEPTION, Status.BAD_REQUEST));
return mapOfExceptionsToErrorCodes;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
Expand All @@ -17,8 +18,10 @@
import com.aol.micro.server.module.ConfigurableModule;
import com.aol.micro.server.testing.RestAgent;

import javax.ws.rs.core.Response;

@Microserver
public class ErrorRunnerTest {
public class ErrorDetailsRunnerTest {

RestAgent rest = new RestAgent();

Expand All @@ -32,7 +35,6 @@ public void startServer(){
.build());

server.start();

}

@After
Expand All @@ -42,13 +44,8 @@ public void stopServer(){

@Test
public void runAppAndBasicTest() throws InterruptedException, ExecutionException{


assertThat(rest.get("http://localhost:8080/simple-app/status/ping").getStatus(),is(400));


Response response = rest.get("http://localhost:8080/simple-app/status/ping");
assertThat(response.getStatus(),is(400));
assertThat(response.readEntity(String.class), containsString("{\"errorCode\":\"EMPTY_REQUEST\",\"message\":\"Error id:"));
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

package app.error.com.aol.micro.server;

import com.aol.micro.server.MicroserverApp;
import com.aol.micro.server.config.Microserver;
import com.aol.micro.server.module.ConfigurableModule;
import com.aol.micro.server.testing.RestAgent;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.core.Response;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@Microserver(properties = {"micro.general.exception.mapper.details", "false"})
public class ErrorNoDetailsRunnerTest {

RestAgent rest = new RestAgent();

MicroserverApp server;
@Before
public void startServer(){
server = new MicroserverApp(ConfigurableModule
.builder()
.context("simple-app")
.defaultResources(Arrays.asList(MultiPartFeature.class))
.build());

server.start();

}

@After
public void stopServer(){
server.stop();
}

@Test
public void runAppAndBasicTest(){
Response response = rest.get("http://localhost:8080/simple-app/status/ping");
assertThat(response.getStatus(),is(400));
assertThat(response.readEntity(String.class), containsString("{\"errorCode\":\"INTERNAL_SERVER_ERROR\",\"message\":"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class GeneralExceptionMapperTest {

@Before
public void setUp() throws Exception {
mapper = new GeneralExceptionMapper();
mockLogger = mock(Logger.class);
mapper = new GeneralExceptionMapper(mockLogger, true);
}

@Test
Expand Down Expand Up @@ -65,7 +65,7 @@ public void whenRuntimeException_thenInternalServerError() {

@Test
public void whenJacksonException_thenNotInternalServerError_NoErrorsLogged() {
mapper= new GeneralExceptionMapper(mockLogger);
mapper= new GeneralExceptionMapper(mockLogger, true);
assertThat(mapper.toResponse(new MyLocalException()).getStatus(), is((Status.INTERNAL_SERVER_ERROR.getStatusCode())));
verify(mockLogger, times(0)).error(any(String.class), any(Object[].class));
verify(mockLogger, times(0)).error(any(String.class), any(Throwable.class), any(Object[].class));
Expand Down

0 comments on commit cf8c75e

Please sign in to comment.