From c88f5b404e6881cafb94beed03c0323f71a3fa9d Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Thu, 23 May 2024 16:14:13 +0200 Subject: 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 --- aai-resources/pom.xml | 2 +- .../java/org/onap/aai/rest/ExceptionHandler.java | 106 ++++++++++----------- pom.xml | 2 +- 3 files changed, 53 insertions(+), 57 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 @@ org.onap.aai.resources resources - 1.13.5-SNAPSHOT + 1.13.6-SNAPSHOT 1.8 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 { + private static final String AAI_4007 = "AAI_4007"; + @Context private HttpServletRequest request; @@ -57,70 +59,64 @@ public class ExceptionHandler implements ExceptionMapper { @Override public Response toResponse(Exception exception) { - Response response = null; - ArrayList templateVars = new ArrayList(); - // 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 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 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 templateVars = new ArrayList<>(); + templateVars.add(customExceptionName); + return buildInputParsingErrorResponse(exception, templateVars); + } + + private Response buildInputParsingErrorResponse(Exception exception, ArrayList 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 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()); + } + } diff --git a/pom.xml b/pom.xml index a6f2876..5add3bb 100644 --- a/pom.xml +++ b/pom.xml @@ -30,7 +30,7 @@ org.onap.aai.resources resources - 1.13.5-SNAPSHOT + 1.13.6-SNAPSHOT aai-resources pom -- cgit 1.2.3-korg