Class ApiExceptionHandler
Base IApiExceptionHandler implementation that reports ApiException instances as-is, applies application-specific mappings via MapAsync(HttpContext, Exception), and handles unexpected exceptions by logging them with a reference ID and reporting a ServerErrorApiException that includes the reference ID. In development environments unexpected exceptions are propagated instead so that the developer exception page can display them.
Implements
Namespace: Singulink.Net.Http.Api.Service
Assembly: Singulink.Net.Http.Api.Service.dll
Syntax
public abstract class ApiExceptionHandler : IApiExceptionHandler
Remarks
Derive from this class to add application-specific exception mappings or customize the unexpected exception handling, or register the default behavior directly via AddApiExceptionHandler(IServiceCollection).
Constructors
| Name | Description |
|---|---|
| ApiExceptionHandler(ILogger, IHostEnvironment) | Initializes a new instance of the ApiExceptionHandler class. |
Properties
| Name | Description |
|---|---|
| PropagateUnexpectedExceptions | Gets a value indicating whether unexpected exceptions should propagate unhandled instead of being logged and reported as a server error. Defaults to true in development environments so that the developer exception page can display them (and streaming responses report full exception details). |
Methods
| Name | Description |
|---|---|
| CreateServerErrorMessage(Guid) | Creates the message reported to the client for an unexpected exception that was logged with the specified reference ID. |
| HandleAsync(HttpContext, Exception) | Maps the specified exception to the ApiException that should be reported to the client. |
| HandleUnexpectedAsync(HttpContext, Exception) | Handles an exception that was not recognized by MapAsync(HttpContext, Exception). The default implementation returns null if PropagateUnexpectedExceptions is true, otherwise it generates a reference ID, logs the exception via LogUnexpected(HttpContext, Exception, Guid) and returns a ServerErrorApiException with the message produced by CreateServerErrorMessage(Guid). |
| LogUnexpected(HttpContext, Exception, Guid) | Logs an unexpected exception. The default implementation logs an error with the request method, path and reference ID as structured properties. |
| MapAsync(HttpContext, Exception) | Maps application-specific exceptions to the ApiException that should be reported to the client (e.g. validation library exceptions to a ValidationApiException). Returns null (the default) if the exception is not recognized, in which case it is treated as unexpected. |