@Override protected void initialize() throws ServletException { // ... define your resource providers here ... // Now register the interceptor ExceptionHandlingInterceptor interceptor = new ExceptionHandlingInterceptor(); registerInterceptor(interceptor); // Return the stack trace to the client for the following exception types interceptor.setReturnStackTracesForExceptionTypes(InternalErrorException.class, NullPointerException.class); }
@Override public boolean handleException(RequestDetails theRequestDetails, BaseServerResponseException theException, HttpServletRequest theRequest, HttpServletResponse theResponse) throws ServletException, IOException { Closeable writer = (Closeable) handleException(theRequestDetails, theException); writer.close(); return false; }
private BaseServerResponseException preprocessException(final Throwable theException, final JaxRsRequest requestDetails) { try { Throwable theExceptionToConvert = theException; if (!(theException instanceof BaseServerResponseException) && (theException.getCause() instanceof BaseServerResponseException)) { theExceptionToConvert = theException.getCause(); } return exceptionHandler.preProcessOutgoingException(requestDetails, theExceptionToConvert, null); } catch (final ServletException e) { return new InternalErrorException(e); } }
exception = DEFAULT_EXCEPTION_HANDLER.preProcessOutgoingException(requestDetails, e, theRequest); DEFAULT_EXCEPTION_HANDLER.handleException(requestDetails, exception, theRequest, theResponse);
/** * The default constructor */ public JaxRsExceptionInterceptor() { this.exceptionHandler = new ExceptionHandlingInterceptor(); }
/** * This method convert an exception to a JaxRsResponseException * @param theServer the provider * @param theException the exception to convert * @return JaxRsResponseException */ public JaxRsResponseException convertException(final AbstractJaxRsProvider theServer, final Throwable theException) { if (theServer.withStackTrace()) { exceptionHandler.setReturnStackTracesForExceptionTypes(Throwable.class); } final JaxRsRequest requestDetails = theServer.getRequest(null, null).build(); final BaseServerResponseException convertedException = preprocessException(theException, requestDetails); return new JaxRsResponseException(convertedException); }
oo = createOperationOutcome(theException, ctx);
populateDetails(ctx, theException, oo); } else if (theException instanceof BaseServerResponseException) { int statusCode = ((BaseServerResponseException) theException).getStatusCode(); populateDetails(ctx, theException, oo); if (baseServerResponseException.getAdditionalMessages() != null) { for (String next : baseServerResponseException.getAdditionalMessages()) { populateDetails(ctx, theException, oo);
exception = DEFAULT_EXCEPTION_HANDLER.preProcessOutgoingException(requestDetails, e, theRequest); DEFAULT_EXCEPTION_HANDLER.handleException(requestDetails, exception, theRequest, theResponse);
@Override public BaseServerResponseException preProcessOutgoingException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest) throws ServletException { BaseServerResponseException retVal; if (theException instanceof DataFormatException) { // Wrapping the DataFormatException as an InvalidRequestException so that it gets sent back to the client as a 400 response. retVal = new InvalidRequestException(theException); } else if (!(theException instanceof BaseServerResponseException)) { retVal = new InternalErrorException(theException); } else { retVal = (BaseServerResponseException) theException; } if (retVal.getOperationOutcome() == null) { retVal.setOperationOutcome(createOperationOutcome(theException, theRequestDetails.getServer().getFhirContext())); } return retVal; }
populateDetails(ctx, theException, oo); } else if (theException instanceof BaseServerResponseException) { int statusCode = ((BaseServerResponseException) theException).getStatusCode(); populateDetails(ctx, theException, oo); if (baseServerResponseException.getAdditionalMessages() != null) { for (String next : baseServerResponseException.getAdditionalMessages()) { populateDetails(ctx, theException, oo);
private Response handleExceptionWithoutServletError(final JaxRsRequest theRequest, final BaseServerResponseException theException) throws IOException { try { return (Response) exceptionHandler.handleException(theRequest, theException); } catch (final ServletException e) { final BaseServerResponseException newException = preprocessException(new InternalErrorException(e), theRequest); return handleExceptionWithoutServletError(theRequest, newException); } } }
oo = createOperationOutcome(theException, ctx);
@Override public boolean handleException(RequestDetails theRequestDetails, BaseServerResponseException theException, HttpServletRequest theRequest, HttpServletResponse theResponse) throws ServletException, IOException { Closeable writer = (Closeable) handleException(theRequestDetails, theException); writer.close(); return false; }
@Override public BaseServerResponseException preProcessOutgoingException(RequestDetails theRequestDetails, Throwable theException, HttpServletRequest theServletRequest) throws ServletException { BaseServerResponseException retVal; if (theException instanceof DataFormatException) { // Wrapping the DataFormatException as an InvalidRequestException so that it gets sent back to the client as a 400 response. retVal = new InvalidRequestException(theException); } else if (!(theException instanceof BaseServerResponseException)) { retVal = new InternalErrorException(theException); } else { retVal = (BaseServerResponseException) theException; } if (retVal.getOperationOutcome() == null) { retVal.setOperationOutcome(createOperationOutcome(theException, theRequestDetails.getServer().getFhirContext())); } return retVal; }