private String getParamExceptionErrorMessage(ParamException pe) { Throwable cause = pe.getCause(); if (cause instanceof ExtractorException && cause.getCause() != null) { // ExtractorException does not have any extra info cause = cause.getCause(); } final String causeMessage = (cause != null) ? " " + cause.getMessage() : ""; return pe.getDefaultStringValue() != null ? String.format("%s: %s %s (default: %s).%s", pe.getMessage(), // generic status message pe.getParameterType().getSimpleName(), pe.getParameterName(), // which param is wrong pe.getDefaultStringValue(), // if it has a default causeMessage) : String.format("%s: %s %s.%s", pe.getMessage(), // generic status message pe.getParameterType().getSimpleName(), pe.getParameterName(), // which param is wrong causeMessage);// the underlying problem }
@Override public Response toResponse(MultiException exc) { LOGGER.debug("mapping " + exc.getClass(), exc); List<Throwable> errors = exc.getErrors(); if (errors.size() > 0) { Throwable cause = errors.get(0); if (cause instanceof QueryParamException || cause instanceof FormParamException) { ParamException paramException = (ParamException) cause; return Response .status(400) .type(MediaType.TEXT_PLAIN) .entity("illegal value for parameter '" + paramException.getParameterName() + "'") .build(); } } return Response.serverError().build(); }
@Override public Response toResponse(ParamException notFoundException) { return Response .status(Response.Status.BAD_REQUEST) .type(getMediaType(headers, MediaType.APPLICATION_JSON_TYPE)) .entity(new ErrorCodeDto(ErrorCodeDto.VMIDC_VALIDATION_EXCEPTION_ERROR_CODE, Arrays.asList("Not found"))) .build(); }
@Override public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) { ApiExceptionHandlerListenerResult result; SortedApiErrorSet handledErrors = null; List<Pair<String, String>> extraDetailsForLogging = new ArrayList<>(); if (ex instanceof ParamException.UriParamException) { utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging); // Returning a 404 is intentional here. // The Jersey contract for URIParamException states it should map to a 404. handledErrors = singletonSortedSetOf(projectApiErrors.getNotFoundApiError()); } else if (ex instanceof ParamException) { utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging); handledErrors = singletonSortedSetOf(projectApiErrors.getMalformedRequestApiError()); } // Return an indication that we will handle this exception if handledErrors got set if (handledErrors != null) { result = ApiExceptionHandlerListenerResult.handleResponse(handledErrors, extraDetailsForLogging); } else { result = ApiExceptionHandlerListenerResult.ignoreResponse(); } return result; }
@DataProvider public static Object[][] dataProviderForShouldHandleException() { return new Object[][] { { mock(ParamException.UriParamException.class), testProjectApiErrors.getNotFoundApiError() }, { mock(ParamException.class), testProjectApiErrors.getMalformedRequestApiError() } }; }
@Override public Response toResponse(final Throwable exception) { if (exception instanceof ParamException.PathParamException) { return Response.status(Response.Status.NOT_FOUND) .header(HttpHeaders.CONTENT_TYPE, ExtraMediaType.TEXT_HTML) .entity(viewFactory.exceptionNotFoundView()) .build(); } if (exception instanceof FieldConversionException) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR) .header(HttpHeaders.CONTENT_TYPE, ExtraMediaType.TEXT_HTML) .entity(viewFactory.exceptionFieldConversionView(exception.getMessage())) .build(); } if (exception instanceof InconsistencyException) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR) .header(HttpHeaders.CONTENT_TYPE, ExtraMediaType.TEXT_HTML) .entity(viewFactory.exceptionInconsistencyView(exception.getMessage())) .build(); } LOGGER.error("Uncaught exception: {}", exception); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) .header(HttpHeaders.CONTENT_TYPE, ExtraMediaType.TEXT_HTML) .entity(viewFactory.exceptionServerErrorView()) .build(); }
@Override public Response toResponse(final ParamException e) { LOGGER.error("ParamException intercepted by ParamExceptionMapper: {}\n", e.getMessage()); debugException(this, e, LOGGER); final StringBuilder msg = new StringBuilder("Error parsing parameter: "); msg.append(e.getParameterName()); msg.append(", of type: "); msg.append(e.getParameterType().getSimpleName()); return fromResponse(e.getResponse()).entity(msg.toString()).type(TEXT_PLAIN_WITH_CHARSET).build(); }
@Test public void testToResponse() { final ParamException input = new ParamException.HeaderParamException(new RuntimeException("canned-exception"), "test-header", null); final Response actual = testObj.toResponse(input); assertEquals(input.getResponse().getStatus(), actual.getStatus()); assertNotNull(actual.getEntity()); }