Package com.linecorp.armeria.server
Interface ServerErrorHandler
- 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 or protocol errors.
Implement this interface to customize Armeria's error responses.
ServerErrorHandler errorHandler = (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 ServerErrorHandler.ofDefault() handle the exception.
return null;
}
Server.builder().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
:
ServerErrorHandler 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 TypeMethodDescriptiondefault ServiceErrorHandler
Transforms thisServerErrorHandler
into aServiceErrorHandler
.static ServerErrorHandler
Returns the defaultServerErrorHandler
.default @Nullable AggregatedHttpResponse
onProtocolViolation
(ServiceConfig config, @Nullable RequestHeaders headers, HttpStatus status, @Nullable String description, @Nullable Throwable cause) Returns anAggregatedHttpResponse
for the protocol error signified by the givenHttpStatus
,message
, andThrowable
.onServiceException
(ServiceRequestContext ctx, Throwable cause) Returns anHttpResponse
for the givenThrowable
raised by a service.default ServerErrorHandler
orElse
(ServerErrorHandler other) Returns a newly createdServerErrorHandler
that tries thisServerErrorHandler
first and then the specifiedServerErrorHandler
when the first call returnsnull
.default @Nullable AggregatedHttpResponse
renderStatus
(@Nullable ServiceRequestContext ctx, ServiceConfig config, @Nullable RequestHeaders headers, HttpStatus status, @Nullable String description, @Nullable Throwable cause)
-
Method Details
-
ofDefault
Returns the defaultServerErrorHandler
. This handler is also used as the final fallback when the handler customized withServerBuilder.errorHandler(ServerErrorHandler)
returnsnull
. For example, the following handler basically delegates all error handling to the default handler:Server .builder() .errorHandler((ctx, cause) -> null) ...
-
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(ServerErrorHandler)
handle the event.
-
onProtocolViolation
@Nullable default @Nullable AggregatedHttpResponse onProtocolViolation(ServiceConfig config, @Nullable @Nullable RequestHeaders headers, HttpStatus status, @Nullable @Nullable String description, @Nullable @Nullable Throwable cause) Returns anAggregatedHttpResponse
for the protocol error signified by the givenHttpStatus
,message
, andThrowable
. This method is invoked once for each request failed at the protocol level.- Parameters:
config
- theServiceConfig
that provides the configuration properties.headers
- the receivedRequestHeaders
, ornull
in case of severe protocol violation.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 asContentTooLargeException
.- Returns:
- an
AggregatedHttpResponse
to send to the client, ornull
to let the next handler specified withorElse(ServerErrorHandler)
handle the event.
-
renderStatus
@Nullable default @Nullable AggregatedHttpResponse renderStatus(@Nullable @Nullable ServiceRequestContext ctx, ServiceConfig config, @Nullable @Nullable RequestHeaders headers, HttpStatus status, @Nullable @Nullable String description, @Nullable @Nullable Throwable cause) Returns anAggregatedHttpResponse
generated from the givenHttpStatus
,message
andThrowable
. Whennull
is returned, the nextServerErrorHandler
in the invocation chain will be used as a fallback (SeeorElse(ServerErrorHandler)
for more information).Note: This method can be invoked by Armeria in combination with the other methods in
ServerErrorHandler
or even independently, and thus should not be used for countingHttpStatusException
s or collecting stats. UseonServiceException(ServiceRequestContext, Throwable)
andonProtocolViolation(ServiceConfig, RequestHeaders, HttpStatus, String, Throwable)
instead.- Parameters:
ctx
- theServiceRequestContext
of the request being handled, ornull
in case of severe protocol violation.config
- theServiceConfig
that provides the configuration properties.headers
- the receivedRequestHeaders
, ornull
in case of severe protocol violation.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 asContentTooLargeException
.- Returns:
- an
AggregatedHttpResponse
, ornull
to let the next handler specified withorElse(ServerErrorHandler)
handle the event.
-
orElse
Returns a newly createdServerErrorHandler
that tries thisServerErrorHandler
first and then the specifiedServerErrorHandler
when the first call returnsnull
.ServerErrorHandler 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; ServerErrorHandler combinedHandler = handler.orElse((ctx, cause) -> { if (cause instanceof SecondException) { 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; // The default handler never returns null. ServerErrorHandler nonNullHandler = combinedHandler.orElse(ServerErrorHandler.ofDefault()); assert handler.onServiceException(ctx, new FirstException()) != null; assert handler.onServiceException(ctx, new SecondException()) != null; assert handler.onServiceException(ctx, new ThirdException()) != null;
-
asServiceErrorHandler
Transforms thisServerErrorHandler
into aServiceErrorHandler
.
-