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

    • 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, 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, 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
                                    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
                                          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.