/** * Maps an exception handler to be executed when an exception occurs during routing * * @param exceptionClass the exception class * @param handler The handler */ public static <T extends Exception> void exception(Class<T> exceptionClass, ExceptionHandler<? super T> handler) { getInstance().exception(exceptionClass, handler); }
/** * Maps an exception handler to be executed when an exception occurs during routing * * @param exceptionClass the exception class * @param handler The handler */ public static <T extends Exception> void exception(Class<T> exceptionClass, ExceptionHandler<? super T> handler) { getInstance().exception(exceptionClass, handler); }
@Override public void initialize() { if (isInitialized()) { return; } service = Service.ignite() .ipAddress(configuration.bindAddress()) .port(configuration.bindPort()); service.path("/v1", () -> { service.get("/version", v1RequestHandler::handleGetVersion); service.get("/status", v1RequestHandler::handleGetStatus); service.delete("/kernel", v1RequestHandler::handleDeleteKernel); } ); service.exception(IllegalArgumentException.class, (exception, request, response) -> { response.status(400); response.type(HttpConstants.CONTENT_TYPE_TEXT_PLAIN_UTF8); response.body(exception.getMessage()); }); service.exception(IllegalStateException.class, (exception, request, response) -> { response.status(500); response.type(HttpConstants.CONTENT_TYPE_TEXT_PLAIN_UTF8); response.body(exception.getMessage()); }); initialized = true; }
service.exception(IllegalArgumentException.class, (exception, request, response) -> { response.status(400); response.type(HttpConstants.CONTENT_TYPE_APPLICATION_JSON_UTF8); response.body(toJson(exception.getMessage())); }); service.exception(ObjectUnknownException.class, (exception, request, response) -> { response.status(404); response.type(HttpConstants.CONTENT_TYPE_APPLICATION_JSON_UTF8); response.body(toJson(exception.getMessage())); }); service.exception(ObjectExistsException.class, (exception, request, response) -> { response.status(409); response.type(HttpConstants.CONTENT_TYPE_APPLICATION_JSON_UTF8); response.body(toJson(exception.getMessage())); }); service.exception(KernelRuntimeException.class, (exception, request, response) -> { response.status(500); response.type(HttpConstants.CONTENT_TYPE_APPLICATION_JSON_UTF8); response.body(toJson(exception.getMessage())); }); service.exception(IllegalStateException.class, (exception, request, response) -> { response.status(500); response.type(HttpConstants.CONTENT_TYPE_APPLICATION_JSON_UTF8);
private void configureExceptionHanding() { service.notFound((req, res) -> ErrorResponder.builder() .statusCode(NOT_FOUND_404) .type(NOT_FOUND) .message(String.format("%s %s can not be found", req.requestMethod(), req.pathInfo())) .asString()); service.internalServerError((req, res) -> ErrorResponder.builder() .statusCode(INTERNAL_SERVER_ERROR_500) .type(SERVER_ERROR) .message("WebAdmin encountered an unexpected internal error") .asString()); service.exception(JsonExtractException.class, (ex, req, res) -> { res.status(BAD_REQUEST_400); res.body(ErrorResponder.builder() .statusCode(BAD_REQUEST_400) .type(INVALID_ARGUMENT) .message("JSON payload of the request is not valid") .cause(ex) .asString()); }); }