Class ExceptionHandler


  • public class ExceptionHandler
    extends java.lang.Object
    Handle exceptions for HTTP responses. Note that using this instead of a proper service handler will easily hide exceptions, or transform unknown exceptions into known exceptions outside of service implementations. But it provides a handy way of normalizing non-default responses onto HTTP status and message format.

    Overridable Methods

    • writeErrorResponse(Throwable, Serializer, HttpServletRequest, HttpServletResponse): Complete handling of exceptions thrown by the PServiceCallHandler.handleCall(PServiceCall, PService) method or similar. Calling super on this will fall back to default exception handling, using the methods below. This will per default serialize PMessage exceptions normally, and just call HttpServletResponse.sendError(int,String) for all the others using the Throwable.getMessage() message.
    • getResponseException(Throwable): Get the response exception given the specific thrown exception. This method can be used to unwrap wrapped exceptions, or transform non- message exceptions into providence message exceptions.
    • statusCodeForException(Throwable): Get the HTTP status code to be used for the error response. Override to specialize, and call super to get default behavior. The default will handle PApplicationException errors, and otherwise return 500 Internal Server Error.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Throwable getResponseException​(java.lang.Throwable e)
      Get the exception to ge handled on failed requests.
      void handleException​(java.lang.Throwable rex, net.morimekta.providence.serializer.Serializer responseSerializer, javax.servlet.http.HttpServletRequest httpRequest, javax.servlet.http.HttpServletResponse httpResponse)
      Handle exceptions from the handle method.
      protected int statusCodeForException​(java.lang.Throwable exception)
      With default exception handling, this can simply change the status code used for the response.
      protected void writeErrorResponse​(java.lang.Throwable rex, net.morimekta.providence.serializer.Serializer responseSerializer, javax.servlet.http.HttpServletRequest httpRequest, javax.servlet.http.HttpServletResponse httpResponse)
      Write the error response.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExceptionHandler

        public ExceptionHandler()
    • Method Detail

      • handleException

        public void handleException​(@Nonnull
                                    java.lang.Throwable rex,
                                    @Nonnull
                                    net.morimekta.providence.serializer.Serializer responseSerializer,
                                    @Nonnull
                                    javax.servlet.http.HttpServletRequest httpRequest,
                                    @Nonnull
                                    javax.servlet.http.HttpServletResponse httpResponse)
                             throws java.io.IOException
        Handle exceptions from the handle method. This method can be overridden if a thrown exception must be handled before it is transformed with getResponseException(Throwable). Otherwise override writeErrorResponse(Throwable, Serializer, HttpServletRequest, HttpServletResponse) instead.
        Parameters:
        rex - The response exception, which is the thrown exception or one of it's causes. See getResponseException(Throwable).
        responseSerializer - The serializer to use to serialize message output.
        httpRequest - The HTTP request.
        httpResponse - The HTTP response.
        Throws:
        java.io.IOException - If writing the response failed.
      • writeErrorResponse

        protected void writeErrorResponse​(@Nonnull
                                          java.lang.Throwable rex,
                                          @Nonnull
                                          net.morimekta.providence.serializer.Serializer responseSerializer,
                                          @Nonnull
                                          javax.servlet.http.HttpServletRequest httpRequest,
                                          @Nonnull
                                          javax.servlet.http.HttpServletResponse httpResponse)
                                   throws java.io.IOException
        Write the error response. Override this method in order to write the response or response headers specifically. Callers must call super if response not written. It is fine to set headers and call super too.
        Parameters:
        rex - The response exception.
        responseSerializer - The response serializer.
        httpRequest - The request.
        httpResponse - The response.
        Throws:
        java.io.IOException - If writing the response failed.
      • getResponseException

        @Nonnull
        protected java.lang.Throwable getResponseException​(java.lang.Throwable e)
        Get the exception to ge handled on failed requests.
        Parameters:
        e - The exception seen.
        Returns:
        The exception to use as response base.
      • statusCodeForException

        protected int statusCodeForException​(@Nonnull
                                             java.lang.Throwable exception)
        With default exception handling, this can simply change the status code used for the response.
        Parameters:
        exception - The exception seen.
        Returns:
        The status code to be used.