Package io.muserver

Interface UnhandledExceptionHandler

    • Method Detail

      • handle

        boolean handle​(MuRequest request,
                       MuResponse response,
                       java.lang.Throwable cause)
                throws java.lang.Exception
        Called when an exception is thrown by another handler.

        Note that if the response has already started sending data, you will not be able to add a custom error message. In this case, you may want to allow for the default error handling by returning false.

        The following shows a pattern to filter out certain errors:

        
         muServerBuilder.withExceptionHandler((request, response, exception) -> {
             if (response.hasStartedSendingData()) return false; // cannot customise the response
             if (exception instanceof NotAuthorizedException) return false;
             response.contentType(ContentTypes.TEXT_PLAIN_UTF8);
             response.write("Oh I'm worry, there was a problem");
             return true;
         })
         
        Parameters:
        request - The request
        response - The response
        cause - The exception thrown by an earlier handler
        Returns:
        true if this handler has written a response; otherwise false in which case the default error handler will be invoked.
        Throws:
        java.lang.Exception - Throwing an exception will result in a 500 error code being returned with a basic error message.