Reputation: 1179
I am using Play 2.2.1. In my Global class I have the onError()-method overridden like this:
public Promise<SimpleResult> onError(RequestHeader request, Throwable t) {
Logger.info("onError reached");
return Promise.<SimpleResult>pure(internalServerError(t.getMessage()));
}
Thus my code is only slightly modified compared to the documentation.
After an exception occurs, it appears in the logging and the "onError reached" is also logged. Nevertheless it seems that the Result is never sent to the client as it is still waiting for an answer.
How do I send the internalServerError-Result back to the client if an error occured?
As a workaround I do the following:
public static Result someAction() {
try{
...
} catch (Exception ex) {
Logger.error(ex.getMessage(), ex);
return internalServerError(ex.getMessage());
}
}
I would really like to avoid that, because I think it's ugly, but at least it seems to work. Are there any possible problems (except that it has to be repeated for every Action) with that approach?
Upvotes: 1
Views: 578
Reputation: 23851
Just rechecked and it worked for me.
One minor issue I noticed - your return statement in onError
lacks a closing brace, but I suppose it is not the issue since your code compiles.
Here's my working code:
controller:
package controllers;
import play.mvc.*;
public class Application extends Controller {
public static Result index() {
throw new RuntimeException("boom!");
}
}
Global:
import play.GlobalSettings;
import play.Logger;
import play.libs.F.Promise;
import play.mvc.Http.RequestHeader;
import play.mvc.Results;
import play.mvc.SimpleResult;
public class Global extends GlobalSettings {
public Promise<SimpleResult> onError(RequestHeader request, Throwable t) {
Logger.info("onError reached");
return Promise.<SimpleResult> pure(Results.internalServerError(t.getMessage()));
}
}
Upvotes: 1