|
| 1 | +package io.jooby; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | + |
| 5 | +import javax.annotation.Nonnull; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import static io.jooby.MediaType.html; |
| 13 | +import static io.jooby.MediaType.json; |
| 14 | +import static io.jooby.MediaType.text; |
| 15 | + |
| 16 | +/** |
| 17 | + * Default error handler with content negotiation support and optionally mute log statement base |
| 18 | + * on status code or exception types. |
| 19 | + * |
| 20 | + * @author edgar |
| 21 | + * @since 2.4.1 |
| 22 | + */ |
| 23 | +public class DefaultErrorHandler implements ErrorHandler { |
| 24 | + |
| 25 | + private Set<StatusCode> muteCodes = new HashSet<>(); |
| 26 | + |
| 27 | + private Set<Class> muteTypes = new HashSet<>(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Generate a log.debug call if any of the status code error occurs as exception. |
| 31 | + * |
| 32 | + * @param statusCodes Status codes to mute. |
| 33 | + * @return This error handler. |
| 34 | + */ |
| 35 | + public @Nonnull DefaultErrorHandler mute(@Nonnull StatusCode... statusCodes) { |
| 36 | + Stream.of(statusCodes).forEach(muteCodes::add); |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Generate a log.debug call if any of the exception types occurs. |
| 42 | + * |
| 43 | + * @param exceptionTypes Exception types to mute. |
| 44 | + * @return This error handler. |
| 45 | + */ |
| 46 | + public @Nonnull DefaultErrorHandler mute(@Nonnull Class<? extends Exception>... exceptionTypes) { |
| 47 | + Stream.of(exceptionTypes).forEach(muteTypes::add); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + @Nonnull @Override public void apply(@Nonnull Context ctx, @Nonnull Throwable cause, |
| 52 | + @Nonnull StatusCode statusCode) { |
| 53 | + Logger log = ctx.getRouter().getLog(); |
| 54 | + if (mute(cause, statusCode)) { |
| 55 | + log.debug(ErrorHandler.errorMessage(ctx, statusCode), cause); |
| 56 | + } else { |
| 57 | + log.error(ErrorHandler.errorMessage(ctx, statusCode), cause); |
| 58 | + } |
| 59 | + |
| 60 | + MediaType type = ctx.accept(Arrays.asList(html, json, text)); |
| 61 | + if (json.equals(type)) { |
| 62 | + String message = Optional.ofNullable(cause.getMessage()).orElse(statusCode.reason()); |
| 63 | + ctx.setResponseType(json) |
| 64 | + .setResponseCode(statusCode) |
| 65 | + .send("{\"message\":\"" + XSS.json(message) + "\",\"statusCode\":" + statusCode.value() |
| 66 | + + ",\"reason\":\"" + statusCode.reason() + "\"}"); |
| 67 | + } else if (text.equals(type)) { |
| 68 | + StringBuilder message = new StringBuilder(); |
| 69 | + message.append(ctx.getMethod()).append(" ").append(ctx.getRequestPath()).append(" "); |
| 70 | + message.append(statusCode.value()).append(" ").append(statusCode.reason()); |
| 71 | + if (cause.getMessage() != null) { |
| 72 | + message.append("\n").append(XSS.json(cause.getMessage())); |
| 73 | + } |
| 74 | + ctx.setResponseType(text) |
| 75 | + .setResponseCode(statusCode) |
| 76 | + .send(message.toString()); |
| 77 | + } else { |
| 78 | + String message = cause.getMessage(); |
| 79 | + StringBuilder html = new StringBuilder("<!doctype html>\n") |
| 80 | + .append("<html>\n") |
| 81 | + .append("<head>\n") |
| 82 | + .append("<meta charset=\"utf-8\">\n") |
| 83 | + .append("<style>\n") |
| 84 | + .append("body {font-family: \"open sans\",sans-serif; margin-left: 20px;}\n") |
| 85 | + .append("h1 {font-weight: 300; line-height: 44px; margin: 25px 0 0 0;}\n") |
| 86 | + .append("h2 {font-size: 16px;font-weight: 300; line-height: 44px; margin: 0;}\n") |
| 87 | + .append("footer {font-weight: 300; line-height: 44px; margin-top: 10px;}\n") |
| 88 | + .append("hr {background-color: #f7f7f9;}\n") |
| 89 | + .append("div.trace {border:1px solid #e1e1e8; background-color: #f7f7f9;}\n") |
| 90 | + .append("p {padding-left: 20px;}\n") |
| 91 | + .append("p.tab {padding-left: 40px;}\n") |
| 92 | + .append("</style>\n") |
| 93 | + .append("<title>") |
| 94 | + .append(statusCode) |
| 95 | + .append("</title>\n") |
| 96 | + .append("<body>\n") |
| 97 | + .append("<h1>").append(statusCode.reason()).append("</h1>\n") |
| 98 | + .append("<hr>\n"); |
| 99 | + |
| 100 | + if (message != null && !message.equals(statusCode.toString())) { |
| 101 | + html.append("<h2>message: ").append(XSS.html(message)).append("</h2>\n"); |
| 102 | + } |
| 103 | + html.append("<h2>status code: ").append(statusCode.value()).append("</h2>\n"); |
| 104 | + |
| 105 | + html.append("</body>\n") |
| 106 | + .append("</html>"); |
| 107 | + |
| 108 | + ctx |
| 109 | + .setResponseType(MediaType.html) |
| 110 | + .setResponseCode(statusCode) |
| 111 | + .send(html.toString()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private boolean mute(Throwable cause, StatusCode statusCode) { |
| 116 | + return muteCodes.contains(statusCode) |
| 117 | + // same class filter |
| 118 | + || muteTypes.stream().anyMatch(type -> type == cause.getClass()) |
| 119 | + // sub-class filter |
| 120 | + || muteTypes.stream().anyMatch(type -> type.isInstance(cause)); |
| 121 | + } |
| 122 | +} |
0 commit comments