diff options
author | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-05-23 16:14:13 +0200 |
---|---|---|
committer | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2024-05-23 16:14:13 +0200 |
commit | c88f5b404e6881cafb94beed03c0323f71a3fa9d (patch) | |
tree | f7caceeca6d5425e77c374c6501e9b9f93ee3400 /aai-resources | |
parent | b74095873ab92c49e88c546f31d0b05b0279b13e (diff) |
Refactor global exception handler
- extract similar code to methods
- return early
- bump snapshot version to 1.13.6
Issue-ID: AAI-3692
Change-Id: I2835a936c3bc60b8f14fd9014de46bdb2367a9fc
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Diffstat (limited to 'aai-resources')
-rw-r--r-- | aai-resources/pom.xml | 2 | ||||
-rw-r--r-- | aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java | 106 |
2 files changed, 52 insertions, 56 deletions
diff --git a/aai-resources/pom.xml b/aai-resources/pom.xml index abfd928..b69b93c 100644 --- a/aai-resources/pom.xml +++ b/aai-resources/pom.xml @@ -28,7 +28,7 @@ <parent> <groupId>org.onap.aai.resources</groupId> <artifactId>resources</artifactId> - <version>1.13.5-SNAPSHOT</version> + <version>1.13.6-SNAPSHOT</version> </parent> <properties> <java.version>1.8</java.version> diff --git a/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java b/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java index ed57a7a..81c49b9 100644 --- a/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java +++ b/aai-resources/src/main/java/org/onap/aai/rest/ExceptionHandler.java @@ -45,6 +45,8 @@ import org.onap.aai.logging.ErrorLogHelper; @Provider public class ExceptionHandler implements ExceptionMapper<Exception> { + private static final String AAI_4007 = "AAI_4007"; + @Context private HttpServletRequest request; @@ -57,70 +59,64 @@ public class ExceptionHandler implements ExceptionMapper<Exception> { @Override public Response toResponse(Exception exception) { - Response response = null; - ArrayList<String> templateVars = new ArrayList<String>(); - // the general case is that cxf will give us a WebApplicationException // with a linked exception if (exception instanceof WebApplicationException) { - WebApplicationException e = (WebApplicationException) exception; - if (e.getCause() != null) { - if (e.getCause() instanceof SAXParseException2) { - templateVars.add("UnmarshalException"); - AAIException ex = new AAIException("AAI_4007", exception); - response = Response - .status(400).entity(ErrorLogHelper - .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars)) - .build(); - } + if (exception.getCause() instanceof SAXParseException2) { + return buildInputParsingErrorResponse(exception, "UnmarshalException"); + } else { + return ((WebApplicationException)exception).getResponse(); } + } else if (exception instanceof JsonParseException) { // jackson does it differently so we get the direct JsonParseException - templateVars.add("JsonParseException"); - AAIException ex = new AAIException("AAI_4007", exception); - response = Response.status(400) - .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars)) - .build(); + return buildInputParsingErrorResponse(exception); } else if (exception instanceof JsonMappingException) { - // jackson does it differently so we get the direct JsonParseException - templateVars.add("JsonMappingException"); - AAIException ex = new AAIException("AAI_4007", exception); - response = Response.status(400) - .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars)) - .build(); + return buildInputParsingErrorResponse(exception); + } else { + return defaultException(exception); } + } - // it didn't get set above, we wrap a general fault here - if (response == null) { + private Response buildInputParsingErrorResponse(Exception exception) { + ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add(exception.getClass().getSimpleName()); + return buildInputParsingErrorResponse(exception, templateVars); + } - Exception actual_e = exception; - if (exception instanceof WebApplicationException) { - WebApplicationException e = (WebApplicationException) exception; - response = e.getResponse(); - } else { - templateVars.add(request.getMethod()); - templateVars.add("unknown"); - AAIException ex = new AAIException("AAI_4000", actual_e); - List<MediaType> mediaTypes = headers.getAcceptableMediaTypes(); - int setError = 0; - - for (MediaType mediaType : mediaTypes) { - if (MediaType.APPLICATION_XML_TYPE.isCompatible(mediaType)) { - response = Response - .status(400).type(MediaType.APPLICATION_XML_TYPE).entity(ErrorLogHelper - .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars)) - .build(); - setError = 1; - } - } - if (setError == 0) { - response = Response - .status(400).type(MediaType.APPLICATION_JSON_TYPE).entity(ErrorLogHelper - .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars)) - .build(); - } - } - } - return response; + private Response buildInputParsingErrorResponse(Exception exception, String customExceptionName) { + ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add(customExceptionName); + return buildInputParsingErrorResponse(exception, templateVars); + } + + private Response buildInputParsingErrorResponse(Exception exception, ArrayList<String> templateVars) { + AAIException ex = new AAIException(AAI_4007, exception); + return Response + .status(400).entity(ErrorLogHelper + .getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), ex, templateVars)) + .build(); } + + private Response defaultException(Exception exception) { + ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add(request.getMethod()); + templateVars.add("unknown"); + AAIException ex = new AAIException("AAI_4000", exception); + // prefer xml, use json otherwise + return headers.getAcceptableMediaTypes().stream() + .filter(MediaType.APPLICATION_ATOM_XML_TYPE::isCompatible) + .findAny() + .map(xmlType -> + Response.status(400).type(MediaType.APPLICATION_XML_TYPE) + .entity(ErrorLogHelper.getRESTAPIErrorResponse( + headers.getAcceptableMediaTypes(), ex, templateVars)) + .build()) + .orElseGet(() -> + Response.status(400).type(MediaType.APPLICATION_JSON_TYPE) + .entity(ErrorLogHelper.getRESTAPIErrorResponse( + headers.getAcceptableMediaTypes(), ex, templateVars)) + .build()); + } + } |