Package io.muserver
Interface UnhandledExceptionHandler
-
public interface UnhandledExceptionHandlerA handler for exceptions that have been thrown by other handlers which allows for custom error pages.This is registered with
MuServerBuilder.withExceptionHandler(UnhandledExceptionHandler).Note:
RedirectionExceptions will not get routed to this handler.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleanhandle(MuRequest request, MuResponse response, java.lang.Throwable cause)Called when an exception is thrown by another handler.
-
-
-
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 requestresponse- The responsecause- The exception thrown by an earlier handler- Returns:
trueif this handler has written a response; otherwisefalsein which case the default error handler will be invoked.- Throws:
java.lang.Exception- Throwing an exception will result in a500error code being returned with a basic error message.
-
-