Package com.linecorp.armeria.server
Interface ExceptionHandler
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Converts a
Throwable
to an AggregatedHttpResponse
.
Use this ExceptionHandler
to send a different response depending on the Throwable
:
ExceptionHandler handler = (ctx, cause) -> {
if (cause instanceof IllegalArgumentException) {
return HttpResponse.of(HttpStatus.BAD_REQUEST);
}
// You can return a different response using the path.
if ("/outage".equals(ctx.path())) {
return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR,
MediaType.PLAIN_TEXT, "Oops, something went wrong.");
}
// Return null to let ExceptionHandler.ofDefault() convert the exception.
return null;
}
Server.builder().exceptionHandler(handler)...
-
Method Summary
Modifier and TypeMethodDescriptionconvert(ServiceRequestContext context, Throwable cause)
Converts the givenThrowable
to anAggregatedHttpResponse
.static ExceptionHandler
Returns the defaultExceptionHandler
.default ExceptionHandler
orElse(ExceptionHandler other)
Creates a newExceptionHandler
that tries thisExceptionHandler
first and then the specifiedExceptionHandler
when thisExceptionHandler
returnsnull
.
-
Method Details
-
ofDefault
Returns the defaultExceptionHandler
. -
convert
Converts the givenThrowable
to anAggregatedHttpResponse
. Whennull
is returned,ofDefault()
converts theThrowable
.- See Also:
orElse(ExceptionHandler)
-
orElse
Creates a newExceptionHandler
that tries thisExceptionHandler
first and then the specifiedExceptionHandler
when thisExceptionHandler
returnsnull
.
-