/** * Setup a route error handler. The error handler will be executed if current status code matches * the one provided. * * All headers are reset while generating the error response. * * @param statusCode The status code to match. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final int statusCode, final Err.Handler handler) { return err((req, rsp, x) -> { if (statusCode == x.statusCode()) { handler.handle(req, rsp, x); } }); }
public Status apply(final Throwable cause) { if (cause instanceof Err) { return Status.valueOf(((Err) cause).statusCode()); } /** * usually a class name, except for inner classes where '$' is replaced it by '.' */ Function<Class<?>, String> name = type -> Optional.ofNullable(type.getDeclaringClass()) .map(dc -> new StringBuilder(dc.getName()) .append('.') .append(type.getSimpleName()) .toString()) .orElse(type.getName()); Config err = conf.getConfig("err"); int status = -1; Class<?> type = cause.getClass(); while (type != Throwable.class && status == -1) { String classname = name.apply(type); if (err.hasPath(classname)) { status = err.getInt(classname); } else { type = type.getSuperclass(); } } return status == -1 ? Status.SERVER_ERROR : Status.valueOf(status); } }
/** * Setup a route error handler. The error handler will be executed if current status code matches * the one provided. * * All headers are reset while generating the error response. * * @param predicate Apply the error handler if the predicate evaluates to <code>true</code>. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Predicate<Status> predicate, final Err.Handler handler) { return err((req, rsp, err) -> { if (predicate.test(Status.valueOf(err.statusCode()))) { handler.handle(req, rsp, err); } }); }
/** * Setup a route error handler. The error handler will be executed if current status code matches * the one provided. * * All headers are reset while generating the error response. * * @param code The status code to match. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Status code, final Err.Handler handler) { return err((req, rsp, x) -> { if (code.value() == x.statusCode()) { handler.handle(req, rsp, x); } }); }
private void cleanup(final Throwable cause) { open = false; NativeWebSocket lws = ws; this.ws = null; this.injector = null; this.handler = null; this.closeCallback = null; this.exceptionCallback = null; this.messageCallback = null; if (lws != null && lws.isOpen()) { WebSocket.CloseStatus closeStatus = WebSocket.SERVER_ERROR; if (cause instanceof IllegalArgumentException) { closeStatus = WebSocket.BAD_DATA; } else if (cause instanceof NoSuchElementException) { closeStatus = WebSocket.BAD_DATA; } else if (cause instanceof Err) { Err err = (Err) cause; if (err.statusCode() == 400) { closeStatus = WebSocket.BAD_DATA; } } lws.close(closeStatus.code(), closeStatus.reason()); } }
public Status apply(final Throwable cause) { if (cause instanceof Err) { return Status.valueOf(((Err) cause).statusCode()); } /** * usually a class name, except for inner classes where '$' is replaced it by '.' */ Function<Class<?>, String> name = type -> Optional.ofNullable(type.getDeclaringClass()) .map(dc -> new StringBuilder(dc.getName()) .append('.') .append(type.getSimpleName()) .toString()) .orElse(type.getName()); Config err = conf.getConfig("err"); int status = -1; Class<?> type = cause.getClass(); while (type != Throwable.class && status == -1) { String classname = name.apply(type); if (err.hasPath(classname)) { status = err.getInt(classname); } else { type = type.getSuperclass(); } } return status == -1 ? Status.SERVER_ERROR : Status.valueOf(status); } }
/** * Setup a route error handler. The error handler will be executed if current status code matches * the one provided. * * All headers are reset while generating the error response. * * @param statusCode The status code to match. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final int statusCode, final Err.Handler handler) { return err((req, rsp, x) -> { if (statusCode == x.statusCode()) { handler.handle(req, rsp, x); } }); }
/** * Setup a route error handler. The error handler will be executed if current status code matches * the one provided. * * All headers are reset while generating the error response. * * @param predicate Apply the error handler if the predicate evaluates to <code>true</code>. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Predicate<Status> predicate, final Err.Handler handler) { return err((req, rsp, err) -> { if (predicate.test(Status.valueOf(err.statusCode()))) { handler.handle(req, rsp, err); } }); }
/** * Setup a route error handler. The error handler will be executed if current status code matches * the one provided. * * All headers are reset while generating the error response. * * @param code The status code to match. * @param handler A route error handler. * @return This router. */ @Nonnull default Router err(final Status code, final Err.Handler handler) { return err((req, rsp, x) -> { if (code.value() == x.statusCode()) { handler.handle(req, rsp, x); } }); }
private void cleanup(final Throwable cause) { open = false; NativeWebSocket lws = ws; this.ws = null; this.injector = null; this.handler = null; this.closeCallback = null; this.exceptionCallback = null; this.messageCallback = null; if (lws != null && lws.isOpen()) { WebSocket.CloseStatus closeStatus = WebSocket.SERVER_ERROR; if (cause instanceof IllegalArgumentException) { closeStatus = WebSocket.BAD_DATA; } else if (cause instanceof NoSuchElementException) { closeStatus = WebSocket.BAD_DATA; } else if (cause instanceof Err) { Err err = (Err) cause; if (err.statusCode() == 400) { closeStatus = WebSocket.BAD_DATA; } } lws.close(closeStatus.code(), closeStatus.reason()); } }