public class JsonHttpErrorHandler extends Object implements HttpErrorHandler
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.
Constructor and Description |
---|
JsonHttpErrorHandler(Environment environment,
play.api.OptionalSourceMapper sourceMapper) |
Modifier and Type | Method and 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 |
formatDevServerErrorException(Throwable exception)
Format a
Throwable 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.
|
@Inject public JsonHttpErrorHandler(Environment environment, play.api.OptionalSourceMapper sourceMapper)
public CompletionStage<Result> onClientError(Http.RequestHeader request, int statusCode, String message)
HttpErrorHandler
onClientError
in interface HttpErrorHandler
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.public CompletionStage<Result> onServerError(Http.RequestHeader request, Throwable exception)
HttpErrorHandler
onServerError
in interface HttpErrorHandler
request
- The request that triggered the server error.exception
- The server error.protected final play.api.UsefulException throwableToUsefulException(Throwable throwable)
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.
protected void logServerError(Http.RequestHeader request, play.api.UsefulException usefulException)
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.
request
- The request that triggered the server error.usefulException
- The server error.protected com.fasterxml.jackson.databind.JsonNode devServerError(Http.RequestHeader request, play.api.UsefulException exception)
request
- The request that triggered the error.exception
- The exception.protected com.fasterxml.jackson.databind.JsonNode formatDevServerErrorException(Throwable exception)
Throwable
as a JSON value.
Override this method if you want to change how exceptions are rendered in Dev mode.
exception
- an exceptionprotected com.fasterxml.jackson.databind.JsonNode prodServerError(Http.RequestHeader request, play.api.UsefulException exception)
Override this rather than #onServerError(RequestHeader, Throwable)
if you don't want
to change Play's debug output when logging errors in dev mode.
request
- The request that triggered the error.exception
- The exception.