简体   繁体   English

使用Seedstack和Undertow管理错误处理

[英]Manage error handling with seedstack and undertow

I need to manage HTTP errors, by seting up a generic error message, in order to hide server side errors in the HTTP response. 我需要通过设置通用错误消息来管理HTTP错误,以便在HTTP响应中隐藏服务器端错误。

I'm using Seedstack and the embeded server undertow. 我正在使用Seedstack和嵌入式服务器undertow。 I've found how manage the errors using HttpHandler, I just didn't found yet how integrate this error handler to SeedStack. 我发现了如何使用HttpHandler管理错误,但我还没有发现如何将此错误处理程序集成到SeedStack中。

My SimpleErrorPageHandler 我的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);
    }
}

The current version of SeedStack doesn't allow to define custom Undertow HTTP handler. 当前版本的SeedStack不允许定义自定义的Undertow HTTP处理程序。 Only the HTTP handler for servlet support is hard-coded. 只有用于servlet支持的HTTP处理程序是硬编码的。 We plan to introduce the capability in the future. 我们计划在将来引入该功能

However, regarding your question, we just merged the ability to specify error pages for specific HTTP status codes or exceptions, as well as a default error page. 但是,关于您的问题,我们只是合并了为特定的HTTP状态代码或异常指定错误页面的功能,以及默认错误页面的功能。 This will be done like that: 这样可以完成:

web:
  server:
    errorPages:
      - location: /errors/404.html
        errorCode: 404
      - location: /errors/415.html
        errorCode: 415
      - location: /errors/default.html

This feature will be released in the upcoming SeedStack 19.7, at the end of July. 该功能将在7月底发布的SeedStack 19.7中发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM