gpt4 book ai didi

java - 在 Play 框架中发送有关服务器错误的电子邮件

转载 作者:行者123 更新时间:2023-11-30 07:07:59 25 4
gpt4 key购买 nike

我开发了一个 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())
);
}
}

最佳答案

This may be useful for the non-fatal errors. But in case of fatal errors this will not work. Its better to use Logging and other process exception monitoring solutions.

关于java - 在 Play 框架中发送有关服务器错误的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39754818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com