Skip to content

AddExceptionHandling ​

Registers the two exception handlers this package owns, in the order they have to run: framework exceptions that map to a status code of their own first, then the fallback that turns everything else into a 500. Both are internal, so this call is the only way to get them, and because the fallback answers every exception, your own handler has to be registered before this call or the failures you throw deliberately all become a 500 too. It registers neither AddMessageLocalization nor AddHttpErrorResponseWriter, and the fallback writes the http-error.500 key as its description rather than failing when no message can be resolved for it.

Usage ​

csharp
using AlmightyShogun.AspNet.Core;

builder.Services
    .AddMessageLocalization(builder.Configuration)
    .AddHttpErrorResponseWriter()
    .AddExceptionHandling();

WebApplication app = builder.Build();

app.UseHttpErrorResponses();

WARNING

Registering the handlers is not enough on its own. UseHttpErrorResponses adds the middleware that runs the chain, and without it every exception escapes unhandled.

Client error mapping ​

This call sets SuppressMapClientErrors on ApiBehaviorOptions. A controller marked [ApiController] otherwise rewrites a bodiless error result, such as a bare NotFound(), into a ProblemDetails body of its own, and UseHttpErrorResponses leaves that alone because it only fills in a response that has no body.

Pass false to keep ProblemDetails for the results MVC produces, so the standardized body applies only to errors raised below MVC.

csharp
using AlmightyShogun.AspNet.Core;

builder.Services.AddExceptionHandling(suppressMapClientErrors: false);

Parameters

suppressMapClientErrors: bool
Whether MVC's client-error mapping is turned off. Left on, an [ApiController] rewrites a bodiless error result into ProblemDetails, which UseHttpErrorResponses then leaves alone.
Default: true

Returns

The same IServiceCollection instance with the exception handlers registered.

Type signature ​

csharp
public IServiceCollection AddExceptionHandling(
    bool suppressMapClientErrors = true
);

All packages are released under the MIT License.