The library provides the middleware to handle exceptions and returns appropriate responses.
For using ExceptionMiddleware please register it:
- Add the reference to the submodule project from your WebApi project.
- Register middleware in the Starup file:
Please use UseExceptionMiddleware(IApplicationBuilder) extension method for services
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseExceptionMiddleware();
...
}
- (Optional) Register known exceptions in the ConfigureServices method:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services
.RegisterException<MyCustomException, MyCustomResponse>()
.WithAction((r, e) => e.CustomErrors = e.CustomErrors)
.WithStatusCode(StatusCodes.Status422UnprocessableEntity);
...
}
If the midlleware is registered in the Configure method, all unhandled exceptions are caught and the '500' error returns with ProblemDetails object in the body. The ProblemDetails.Title is set to exception.Message. But there are some ways to customize both the status code and the response body.
- Register the specific exception:
...
services
.RegisterException<MyCustomException>()
.WithStatusCode(StatusCodes.Status422UnprocessableEntity);
...
- Register the specific exception and response type:
...
services
.RegisterException<MyCustomException, MyCustomResponse>()
.WithStatusCode(StatusCodes.Status422UnprocessableEntity);
...
Please note that the response type should be an inheritor on the ProblemDetails class.
- Register the specific exception and response type and some custom fields in the response type:
...
services
.RegisterException<MyCustomException, MyCustomResponse>()
.WithAction((r, e) => r.SomeResponseData = e.SomeExceptionData)
.WithStatusCode(StatusCodes.Status422UnprocessableEntity);
...