Error Handling¶
MS Learn: Handle errors in ASP.NET Core
Ocelot has custom error handling for Exception objects.
Thus, we override the standard error handling provided by ASP.NET Core, which is based on manipulating Exception objects.
Middleware¶
Class: ExceptionHandlerMiddleware
The ExceptionHandlerMiddleware produces the following status codes, in fallback order, after setting the Request ID:
Native response status: Returned when no exception is present, or when a mapped error status is available (excluding
499and500).499 Client Closed Request: A custom Ocelot status returned when an
OperationCanceledExceptionoccurs due to an aborted request. A warning is logged.500 Internal Server Error: The standard status returned when a generic
Exceptionoccurs and Ocelot does not process or map the error. An error record is logged.
Ocelot returns HTTP status codes based on internal logic in specific cases of Client Error Responses and Server Error Responses.
Client Error Responses¶
401 Unauthorized: If the authentication middleware runs and the user is not authenticated.
403 Forbidden: If the authorization middleware runs and the user is unauthorized, if the claim value is not authorized, if the scope is not authorized, if the user does not have the required claim, or if the claim cannot be found.
404 Not Found: If a downstream route cannot be found, or if Ocelot is unable to map an internal error code to an HTTP status code.
499 Client Closed Request: If the request is canceled by the client.
Ocelot Error: RequestCanceledErrorOcelot Code: OcelotErrorCode.RequestCanceledAccording to Ocelot Core’s design, HTTP status code
499is returned in the followingOperationCanceledExceptionscenarios:By
ExceptionHandlerMiddleware, if anOperationCanceledExceptionis thrown and the context’s cancellation token is in the “cancellation requested” state. Ocelot logs a warning with the exception body. If the response has not started, the status code will be set to499.By
ResponderMiddleware, if the defaultIErrorsToHttpStatusCodeMapperservice maps the detected OcelotErrorCode.RequestCanceled to status499. This error code is produced by theIExceptionToErrorMapperservice when anOperationCanceledExceptionis thrown by other middlewares.
Server Error Responses¶
500 Internal Server Error: If unable to complete the HTTP request to the downstream service, and the exception is not
OperationCanceledExceptionorHttpRequestException.502 Bad Gateway: If unable to connect to the downstream service.
503 Service Unavailable: Returned when the downstream request times out.
Ocelot Error: RequestTimedOutErrorOcelot Code: OcelotErrorCode.RequestTimedOutErrorAccording to Ocelot Core’s design, status code
503is produced in the followingTimeoutExceptionscenarios:By
TimeoutDelegatingHandlerfrom theIMessageInvokerPoolservice, when anOperationCanceledExceptionis thrown and the context’s cancellation token is not in the “cancellation requested” state. Ocelot does not log an error with the exception body, but theIExceptionToErrorMapperservice generates the internal OcelotErrorCode.RequestTimedOutError.By
ResponderMiddleware, if the defaultIErrorsToHttpStatusCodeMapperservice maps the detected OcelotErrorCode.RequestTimedOutError to status503. This error code is produced by theIExceptionToErrorMapperservice when aTimeoutExceptionis thrown by other middlewares—especially byTimeoutDelegatingHandler.
Error Mapper¶
Class: HttpExceptionToErrorMapper
Historically, Ocelot errors are implemented by the Exception-to-Error mapper.
The Map method converts an Exception object to a native Ocelot.Errors.Error object.
We override HTTP status codes because of Exception-to-Error mapping.
This can be confusing for the developer since the actual status code of the downstream service may be different and get lost.
Please research and review all response headers of the upstream service.
If you do not find status codes and/or required headers, then the Headers Transformation feature should help.
We expect you to share your use case with us in the Discussions space of the repository. ![]()