An open API service indexing awesome lists of open source software.

https://github.com/floppyshelf/problemize

This package offers a robust, extensible framework for centralized exception handling in .NET Web APIs. It standardizes error responses with ProblemDetails, supports custom exceptions, and features flexible status code mapping. Integrated via middleware, it ensures clean, maintainable global error handling.
https://github.com/floppyshelf/problemize

api aspnetcore centralized-logging custom-exceptions error-handling exception-handling extensibility global-exception-handlers maintainable middleware problem-details robust statuscode-mapping validation-errors webapi

Last synced: 15 days ago
JSON representation

This package offers a robust, extensible framework for centralized exception handling in .NET Web APIs. It standardizes error responses with ProblemDetails, supports custom exceptions, and features flexible status code mapping. Integrated via middleware, it ensures clean, maintainable global error handling.

Awesome Lists containing this project

README

          

![FloppyShelf.Problemize](logo/Problemize.png "FloppyShelf.Problemize")

# Problemize

This package provides a robust and extensible framework for centralized exception handling in .NET Web APIs. It standardizes error responses using ProblemDetails, supports custom exceptions and includes a flexible status code mapping system. The package integrates seamlessly via middleware and service configuration, making global error handling clean and maintainable.

## Features

`IStatusCodeMapper`
An interface to decouple the logic of mapping exceptions to HTTP status codes. Makes it easy to extend or override.

`StatusCodeMapper`
Default implementation of `IStatusCodeMapper`, handling common .NET exceptions like `ArgumentException`, `UnauthorizedAccessException` and `NotImplementedException`.

`ExceptionHandler`
Implements `IExceptionHandler` from ASP.NET Core. This service:
- Maps the exception to an HTTP status code.
- Converts exceptions into a standardized `ProblemDetails` or `ValidationProblemDetails`.
- Writes the response using `IProblemDetailsService`.

`Configurator`
Contains extension methods for:
- Registering services
- Configuring middleware

## Setup Instructions

1. Register services in `Program.cs`
```csharp
builder.Services.UseExceptionHandling();
```

2. Configure middleware in `Program.cs`
```csharp
app.UseExceptionHandling();
```

## Sample ProblemDetails response
```json
{
"title": "An error occured while validating your request",
"detail": "Invalid data",
"type": "ValidationException",
"status": 400,
"instance": "POST /api/users",
"extensions": {
"requestId": "00-abcd1234...",
"activityId": "00-xyz5678..."
}
}
```