Following the docs I was able to implement custom error handlers in my application by overriding methods in the GlobalSettings class:
@Override
public Promise<Result> onBadRequest(RequestHeader request, String error) {
return Promise.<Result> pure(Results.notFound(com.me.project.views.html.pages.ErrorPage
.render(Constants.HTTP_400)));
}
@Override
public Promise<Result> onHandlerNotFound(RequestHeader request) {
return Promise.<Result> pure(Results.notFound(com.me.project.views.html.pages.ErrorPage
.render(Constants.HTTP_404)));
}
@Override
public Promise<Result> onError(RequestHeader request, Throwable t) {
return Promise.<Result> pure(Results.notFound(com.me.project.views.html.pages.ErrorPage
.render(Constants.HTTP_500)));
}
However I only want these to be used in production and not in development, as the errors being displayed in the browser are quite useful.
I am able to detect whether the application is in dev mode using:
boolen isDev = play.api.Play.isDev(play.api.Play.current());
However I cannot call the default error pages as I do not know how. How do you call the default error pages? I have looked at the code here, but my Scala is poor and I cannot seem to get it working. The parts that I though I should port to java in my global object were:
private def defaultErrorHandler: HttpErrorHandler = {
Play.maybeApplication.fold[HttpErrorHandler](DefaultHttpErrorHandler) { app =>
app.injector.instanceOf[DefaultHttpErrorHandler]
}
}
and
def onError(request: RequestHeader, ex: Throwable): Future[Result] =
defaultErrorHandler.onServerError(request, ex)
def onHandlerNotFound(request: RequestHeader): Future[Result] =
defaultErrorHandler.onClientError(request, play.api.http.Status.NOT_FOUND)
def onBadRequest(request: RequestHeader, error: String): Future[Result] =
defaultErrorHandler.onClientError(request, play.api.http.Status.BAD_REQUEST, error)
But I cannot port this as things like Play.maybeApplication
cannot be resoled (are they protected)?!?
I'm sure this should be an easy one but I'm a bit stuck.
Also as a side note can anyone explain the existence of these docs that seem to suggest that the error handling will completely change in 2.4.x :S, I assume that this question will be short lived until 2.4 gets released?
If you browse GlobalSettings.java sources, you will find out, the default implementations of
onError(RequestHeader request, Throwable t)
andonHandlerNotFound(RequestHeader request)
return null, thus you can override these methods this way: