我开发了一个Web服务,我想在发生某些服务器错误时通知我(例如,除零,内存异常等)。我想知道在自定义ErrorHandler
中为每个请求处理和发送此错误是否是一个好习惯?
package controllers;
import play.*;
import play.api.OptionalSourceMapper;
import play.api.UsefulException;
import play.api.routing.Router;
import play.http.DefaultHttpErrorHandler;
import play.mvc.Http.*;
import play.mvc.*;
import javax.inject.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
@Singleton
public class ErrorHandler extends DefaultHttpErrorHandler {
@Inject
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
super(configuration, environment, sourceMapper, routes);
}
// Invoked in prod mode when a server error occurs.
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
return CompletableFuture.completedFuture(Results.internalServerError("A server error occurred (prod): " + exception.getMessage())
// Send email
);
}
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
return CompletableFuture.completedFuture(
Results.internalServerError("A server error occurred (dev): " + exception.getMessage())
);
}
}
答案 0 :(得分:1)
这可能对非致命错误有用。但如果发生致命错误,这将无效。最好使用Logging和其他进程异常监控解决方案。