Package play.http

Class DefaultHttpErrorHandler

  • All Implemented Interfaces:
    HttpErrorHandler

    public class DefaultHttpErrorHandler
    extends Object
    implements HttpErrorHandler
    Default implementation of the http error handler.

    This class is intended to be extended to allow reusing Play's default error handling functionality.

    The "play.editor" configuration setting is used here to give a link back to the source code when set and development mode is on.

    • Constructor Detail

      • DefaultHttpErrorHandler

        @Inject
        public DefaultHttpErrorHandler​(com.typesafe.config.Config config,
                                       Environment environment,
                                       play.api.OptionalSourceMapper sourceMapper,
                                       javax.inject.Provider<play.api.routing.Router> routes)
    • Method Detail

      • onClientError

        public CompletionStage<Result> onClientError​(Http.RequestHeader request,
                                                     int statusCode,
                                                     String message)
        Invoked when a client error occurs, that is, an error in the 4xx series.

        The base implementation calls onBadRequest, onForbidden, onNotFound, or onOtherClientError depending on the HTTP status code.

        Specified by:
        onClientError in interface HttpErrorHandler
        Parameters:
        request - The request that caused the client error.
        statusCode - The error status code. Must be greater or equal to 400, and less than 500.
        message - The error message.
        Returns:
        a CompletionStage containing the Result.
      • onBadRequest

        protected CompletionStage<Result> onBadRequest​(Http.RequestHeader request,
                                                       String message)
        Invoked when a client makes a bad request.

        Returns Results.badRequest (400) with the included template from views.html.defaultpages.badRequest as the content.

        Parameters:
        request - The request that was bad.
        message - The error message.
        Returns:
        a CompletionStage containing the Result.
      • onForbidden

        protected CompletionStage<Result> onForbidden​(Http.RequestHeader request,
                                                      String message)
        Invoked when a client makes a request that was forbidden.

        Returns Results.forbidden (401) with the included template from views.html.defaultpages.unauthorized as the content.

        Parameters:
        request - The forbidden request.
        message - The error message.
        Returns:
        a CompletionStage containing the Result.
      • onNotFound

        protected CompletionStage<Result> onNotFound​(Http.RequestHeader request,
                                                     String message)
        Invoked when a handler or resource is not found.

        If the environment's mode is production, then returns Results.notFound (404) with the included template from `views.html.defaultpages.notFound` as the content.

        Otherwise, Results.notFound (404) is rendered with views.html.defaultpages.devNotFound template.

        Parameters:
        request - The request that no handler was found to handle.
        message - A message, which is not used by the default implementation.
        Returns:
        a CompletionStage containing the Result.
      • onOtherClientError

        protected CompletionStage<Result> onOtherClientError​(Http.RequestHeader request,
                                                             int statusCode,
                                                             String message)
        Invoked when a client error occurs, that is, an error in the 4xx series, which is not handled by any of the other methods in this class already.

        The base implementation uses views.html.defaultpages.badRequest template with the given status.

        Parameters:
        request - The request that caused the client error.
        statusCode - The error status code. Must be greater or equal to 400, and less than 500.
        message - The error message.
        Returns:
        a CompletionStage containing the Result.
      • fatalErrorMessage

        protected String fatalErrorMessage​(Http.RequestHeader request,
                                           Throwable exception)
        Invoked when handling a server error with this error handler failed.

        As a last resort this method allows you to return a (simple) error message that will be send along with a "500 Internal Server Error" response. It's highly recommended to just return a simple string, without doing any fancy processing inside the method (like accessing files,...) that could throw exceptions. This is your last chance to send a meaningful error message when everything else failed.

        Parameters:
        request - The request that triggered the server error.
        exception - The server error.
        Returns:
        An error message which will be send as last resort in case handling a server error with this error handler failed.
      • logServerError

        protected void logServerError​(Http.RequestHeader request,
                                      play.api.UsefulException usefulException)
        Responsible for logging server errors.

        The base implementation uses a SLF4J Logger. If a special annotation is desired for internal server errors, you may want to use SLF4J directly with the Marker API to distinguish server errors from application errors.

        This can also be overridden to add additional logging information, eg. the id of the authenticated user.

        Parameters:
        request - The request that triggered the server error.
        usefulException - The server error.
      • throwableToUsefulException

        protected final play.api.UsefulException throwableToUsefulException​(Throwable throwable)
        Convert the given exception to an exception that Play can report more information about.

        This will generate an id for the exception, and in dev mode, will load the source code for the code that threw the exception, making it possible to report on the location that the exception was thrown from.

      • onDevServerError

        protected CompletionStage<Result> onDevServerError​(Http.RequestHeader request,
                                                           play.api.UsefulException exception)
        Invoked in dev mode when a server error occurs. Note that this method is where the URL set by play.editor is used.

        The base implementation returns Results.internalServerError with the content of views.html.defaultpages.devError.

        Parameters:
        request - The request that triggered the error.
        exception - The exception.
        Returns:
        a CompletionStage containing the Result.
      • onProdServerError

        protected CompletionStage<Result> onProdServerError​(Http.RequestHeader request,
                                                            play.api.UsefulException exception)
        Invoked in prod mode when a server error occurs.

        The base implementation returns Results.internalServerError with the content of views.html.defaultpages.error template.

        Override this rather than onServerError(RequestHeader, Throwable) if you don't want to change Play's debug output when logging errors in dev mode.

        Parameters:
        request - The request that triggered the error.
        exception - The exception.
        Returns:
        a CompletionStage containing the Result.