/** * Setup a custom error handler.The error handler will be executed if the current exception is an * instance of given type type. * * All headers are reset while generating the error response. * * @param type Exception type. The error handler will be executed if the current exception is an * instance of this type. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Class<? extends Throwable> type, final Err.Handler handler) { return err((req, rsp, x) -> { if (type.isInstance(x) || type.isInstance(x.getCause())) { handler.handle(req, rsp, x); } }); }
/** * Produces a friendly view of the err, resulting map has these attributes: * * <pre> * message: exception message (if present) * stacktrace: array with the stacktrace * status: status code * reason: a status code reason * </pre> * * @param stacktrace True for adding stacktrace. * @return A lightweight view of the err. */ public Map<String, Object> toMap(boolean stacktrace) { Status status = Status.valueOf(this.status); Throwable cause = Optional.ofNullable(getCause()).orElse(this); String message = Optional.ofNullable(cause.getMessage()).orElse(status.reason()); Map<String, Object> err = new LinkedHashMap<>(); err.put("message", message); if (stacktrace) { err.put("stacktrace", Throwables.getStackTraceAsString(cause).replace("\r", "").split("\\n")); } err.put("status", status.value()); err.put("reason", status.reason()); return err; }
Throwable cause = Optional.ofNullable(err.getCause()).orElse(err);
/** * Setup a custom error handler.The error handler will be executed if the current exception is an * instance of given type type. * * All headers are reset while generating the error response. * * @param type Exception type. The error handler will be executed if the current exception is an * instance of this type. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Class<? extends Throwable> type, final Err.Handler handler) { return err((req, rsp, x) -> { if (type.isInstance(x) || type.isInstance(x.getCause())) { handler.handle(req, rsp, x); } }); }
/** * Produces a friendly view of the err, resulting map has these attributes: * * <pre> * message: exception message (if present) * stacktrace: array with the stacktrace * status: status code * reason: a status code reason * </pre> * * @param stacktrace True for adding stacktrace. * @return A lightweight view of the err. */ public Map<String, Object> toMap(boolean stacktrace) { Status status = Status.valueOf(this.status); Throwable cause = Optional.ofNullable(getCause()).orElse(this); String message = Optional.ofNullable(cause.getMessage()).orElse(status.reason()); Map<String, Object> err = new LinkedHashMap<>(); err.put("message", message); if (stacktrace) { err.put("stacktrace", Throwables.getStackTraceAsString(cause).replace("\r", "").split("\\n")); } err.put("status", status.value()); err.put("reason", status.reason()); return err; }
Throwable cause = Optional.ofNullable(err.getCause()).orElse(err);