I'm trying to override the default behavior for handling errors in the play framework 2.1.1 but I'm having problems with 404s, basically my overridden onHandlerNotFound doesn't seem to be being called. When I visit a page that doesn't exist all that happens is a blank page is returned. Does anyone know what I'm doing wrong? My Global.java is below
import play.GlobalSettings;
import play.mvc.Http.RequestHeader;
import play.mvc.Result;
import play.mvc.Results;
import views.html.error;
public class Global extends GlobalSettings {
@Override
public Result onHandlerNotFound(RequestHeader request) {
return Results.notFound(error.render());
}
@Override
public Result onError(RequestHeader request, Throwable t) {
return Results.internalServerError(error.render());
}
}
By the way, the onError override is working. Thanks
OK, so the problem in this case was that the route for assets had been changed to
GET /*file controllers.Assets.at(path="/public", file)
changing it back to the default of
GET /assets/*file controllers.Assets.at(path="/public", file)
made it work again. I don't fully understand why it stopped working, can anyone explain?
Your code is valid and works, but the behaviour should be caused by the browser. I tested this by using your code and going to a non-existing page in Firefox and the error page is rendered and shown.
When I try the same with Internet Explorer, it breaks (empty page).
The solution is relatively simple, use an ok instead of notFound:
@Override
public Result onHandlerNotFound(RequestHeader request) {
return Results.ok(error.render());
}
The same goes for a badRequest, I haven't tested any others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With