Package com.linecorp.armeria.server
Interface ServiceErrorHandler
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Provides the error responses in case of unexpected exceptions.
Implement this interface to customize Armeria's error responses.
ServiceErrorHandler errorHandler = (ctx, cause) -> {
if (cause instanceof IllegalArgumentException) {
return HttpResponse.of(HttpStatus.BAD_REQUEST);
}
// Return null to let ServerErrorHandler.ofDefault() handle the exception.
return null;
}
Server.builder().route().errorHandler(errorHandler)...
Recording a service exception (or not)
By default, an exception raised by a service or a decorator is captured and recorded into
RequestLog.responseCause()
. You can keep Armeria from recording it while sending the
desired response by returning a failed response whose cause is an HttpStatusException
or
HttpResponseException
:
ServiceErrorHandler errorHandler = (ctx, cause) -> {
if (cause instanceof IllegalArgumentException) {
// IllegalArgumentException is captured into RequestLog#responseCause().
return HttpResponse.of(HttpStatus.BAD_REQUEST);
}
if (cause instanceof NotFoundException) {
// NotFoundException is NOT captured into RequestLog#responseCause().
return HttpResponse.ofFailure(HttpStatusException.of(HttpStatus.NOT_FOUND));
}
...
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiononServiceException
(ServiceRequestContext ctx, Throwable cause) Returns anHttpResponse
for the givenThrowable
raised by a service.default ServiceErrorHandler
orElse
(ServiceErrorHandler other) Returns a newly createdServiceErrorHandler
that tries thisServiceErrorHandler
first and then the specifiedServiceErrorHandler
when the first call returnsnull
.default @Nullable AggregatedHttpResponse
renderStatus
(ServiceRequestContext ctx, RequestHeaders headers, HttpStatus status, @Nullable String description, @Nullable Throwable cause)
-
Method Details
-
onServiceException
Returns anHttpResponse
for the givenThrowable
raised by a service. This method is invoked once for each request failed with a service-level exception.- Parameters:
ctx
- theServiceRequestContext
of the current request.cause
- theThrowable
raised by the service that handled the current request.- Returns:
- an
HttpResponse
to send to the client, ornull
to let the next handler specified withorElse(ServiceErrorHandler)
handle the event.
-
renderStatus
@Nullable default @Nullable AggregatedHttpResponse renderStatus(ServiceRequestContext ctx, RequestHeaders headers, HttpStatus status, @Nullable @Nullable String description, @Nullable @Nullable Throwable cause) Returns anAggregatedHttpResponse
generated from the givenHttpStatus
,message
andThrowable
. Whennull
is returned, the nextServiceErrorHandler
in the invocation chain will be used as a fallback (SeeorElse(ServiceErrorHandler)
for more information).- Parameters:
ctx
- theServiceRequestContext
of the request being handled.headers
- the receivedRequestHeaders
.status
- the desiredHttpStatus
of the error response.description
- an optional human-readable description of the error.cause
- an optional exception that may contain additional information about the error, such asIllegalArgumentException
.- Returns:
- an
AggregatedHttpResponse
, ornull
to let the next handler specified withorElse(ServiceErrorHandler)
handle the event.
-
orElse
Returns a newly createdServiceErrorHandler
that tries thisServiceErrorHandler
first and then the specifiedServiceErrorHandler
when the first call returnsnull
.ServiceErrorHandler handler = (ctx, cause) -> { if (cause instanceof FirstException) { return HttpResponse.of(200); } return null; } assert handler.onServiceException(ctx, new FirstException()) != null; assert handler.onServiceException(ctx, new SecondException()) == null; assert handler.onServiceException(ctx, new ThirdException()) == null; ServiceErrorHandler combinedHandler = handler.orElse((ctx, cause) -> { if (cause instanceof SecondException) { return HttpResponse.of(200); } return null; }); assert combinedHandler.onServiceException(ctx, new FirstException()) != null; assert combinedHandler.onServiceException(ctx, new SecondException()) != null; assert combinedHandler.onServiceException(ctx, new ThirdException()) == null; // The default handler never returns null. ServiceErrorHandler nonNullHandler = combinedHandler.orElse(ServiceErrorHandler.ofDefault()); assert nonNullHandler.onServiceException(ctx, new FirstException()) != null; assert nonNullHandler.onServiceException(ctx, new SecondException()) != null; assert nonNullHandler.onServiceException(ctx, new ThirdException()) != null;
-