我需要通过设置通用错误消息来管理HTTP错误,以便在HTTP响应中隐藏服务器端错误。
我正在使用Seedstack和嵌入式服务器。我已经找到了如何使用HttpHandler管理错误,但还没有发现如何将此错误处理程序集成到SeedStack。
我的SimpleErrorPageHandler
public class SimpleErrorPageHandler implements HttpHandler {
private final HttpHandler next;
public SimpleErrorPageHandler(final HttpHandler next) {
this.next = next;
}
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.addDefaultResponseListener(exchange1 -> {
if (!exchange1.isResponseChannelAvailable()) {
return false;
}
if (exchange1.getStatusCode() == 500) {
final String errorPage = "<html><head><title>Error</title></head><body>Internal Error</body></html>";
exchange1.getResponseHeaders().put(Headers.CONTENT_LENGTH, "" + errorPage.length());
exchange1.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
Sender sender = exchange1.getResponseSender();
sender.send(errorPage);
return true;
}
return false;
});
next.handleRequest(exchange);
}
}