Interface ErrorHandler<E extends Exception>
- Type Parameters:
E- exception type handled
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
app.error(NoSuchElementException.class, (ctx, failure) -> { throw new NotFoundException(); });
app.error(QuotaExceeded.class, (ctx, failure) -> ctx.status(429).json(new Quota(failure.limit())));
Error handlers run after every middleware has unwound, on the request's thread and under its
deadline, with the request's context; the context's status is reset to 200 first. The response is
encoded by the installed codecs without an Accept check: an error is never answered 406, because
the client would then lose the error itself. Throwing an AxiomException answers with that exception's problem response; that
exception is not offered to error handlers again. Any other exception, or a null result,
is logged with the request ID and answered with the generic 500 problem response. The original
exception is logged once, with the request ID, unless it is an AxiomException below 500
answered below 500; see Application.failureLog(java.lang.System.Logger, java.lang.System.Logger.Level). Error
handlers never run for a cancelled or expired request, and are never offered InterruptedException or CancellationException.
Middleware have already unwound when an error handler runs, so headers they add after
next.run() are not on its response by themselves; each middleware that was entered decorates the
response through Middleware.afterError(com.jsgalactic.axiom.context.Context, com.jsgalactic.axiom.http.Response), as it does for problem responses.
Everything the returned response contains reaches the client: never copy exception messages, class names or stack traces into it.
Lifecycle and thread safety. Registered before startup; the application holds the instance until it closes. One instance serves every request concurrently, so implementations must be thread-safe. The context follows the handler's confinement rules.
-
Method Summary
-
Method Details
-
handle
Maps one failure to a response.- Parameters:
context- the failed request's context, status reset to 200failure- the exception thrown by the handler or a middleware- Returns:
- the response to send; never null
- Throws:
Exception- anAxiomExceptionto answer with its problem response; anything else produces the generic 500
-