Class JsonHttpErrorHandler
- Object
-
- play.http.JsonHttpErrorHandler
-
- All Implemented Interfaces:
HttpErrorHandler
public class JsonHttpErrorHandler extends Object implements HttpErrorHandler
An alternative default HTTP error handler which will render errors as JSON messages instead of HTML pages.In Dev mode, exceptions thrown by the server code will be rendered in JSON messages. In Prod mode, they will not be rendered.
You could override how exceptions are rendered in Dev mode by extending this class and overriding the [[formatDevServerErrorException]] method.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface play.http.HttpErrorHandler
HttpErrorHandler.Attrs
-
-
Constructor Summary
Constructors Constructor Description JsonHttpErrorHandler(Environment environment, play.api.OptionalSourceMapper sourceMapper)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.fasterxml.jackson.databind.JsonNode
devServerError(Http.RequestHeader request, play.api.UsefulException exception)
Invoked in dev mode when a server error occurs.protected com.fasterxml.jackson.databind.JsonNode
fatalErrorJson(Http.RequestHeader request, Throwable exception)
Invoked when handling a server error with this error handler failed.protected com.fasterxml.jackson.databind.JsonNode
formatDevServerErrorException(Throwable exception)
Format aThrowable
as a JSON value.protected void
logServerError(Http.RequestHeader request, play.api.UsefulException usefulException)
Responsible for logging server errors.CompletionStage<Result>
onClientError(Http.RequestHeader request, int statusCode, String message)
Invoked when a client error occurs, that is, an error in the 4xx series.CompletionStage<Result>
onServerError(Http.RequestHeader request, Throwable exception)
Invoked when a server error occurs.protected com.fasterxml.jackson.databind.JsonNode
prodServerError(Http.RequestHeader request, play.api.UsefulException exception)
Invoked in prod mode when a server error occurs.protected play.api.UsefulException
throwableToUsefulException(Throwable throwable)
Convert the given exception to an exception that Play can report more information about.
-
-
-
Constructor Detail
-
JsonHttpErrorHandler
@Inject public JsonHttpErrorHandler(Environment environment, play.api.OptionalSourceMapper sourceMapper)
-
-
Method Detail
-
onClientError
public CompletionStage<Result> onClientError(Http.RequestHeader request, int statusCode, String message)
Description copied from interface:HttpErrorHandler
Invoked when a client error occurs, that is, an error in the 4xx series.- Specified by:
onClientError
in interfaceHttpErrorHandler
- 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 with the Result.
-
onServerError
public CompletionStage<Result> onServerError(Http.RequestHeader request, Throwable exception)
Description copied from interface:HttpErrorHandler
Invoked when a server error occurs.- Specified by:
onServerError
in interfaceHttpErrorHandler
- Parameters:
request
- The request that triggered the server error.exception
- The server error.- Returns:
- a CompletionStage with the Result.
-
fatalErrorJson
protected com.fasterxml.jackson.databind.JsonNode fatalErrorJson(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 JsonNode, 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 JSON which will be send as last resort in case handling a server error with this error handler failed.
-
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.
-
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.
-
devServerError
protected com.fasterxml.jackson.databind.JsonNode devServerError(Http.RequestHeader request, play.api.UsefulException exception)
Invoked in dev mode when a server error occurs.- Parameters:
request
- The request that triggered the error.exception
- The exception.
-
formatDevServerErrorException
protected com.fasterxml.jackson.databind.JsonNode formatDevServerErrorException(Throwable exception)
Format aThrowable
as a JSON value.Override this method if you want to change how exceptions are rendered in Dev mode.
- Parameters:
exception
- an exception- Returns:
- a JSON representation of the passed exception
-
prodServerError
protected com.fasterxml.jackson.databind.JsonNode prodServerError(Http.RequestHeader request, play.api.UsefulException exception)
Invoked in prod mode when a server error occurs.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.
-
-