I need to manage HTTP errors, by seting up a generic error message, in order to hide server side errors in the HTTP response.
I'm using Seedstack and the embeded server undertow. I've found how manage the errors using HttpHandler, I just didn't found yet how integrate this error handler to SeedStack.
My 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. Only the HTTP handler for servlet support is hard-coded. 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. 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.