Skip to content

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.

License

Notifications You must be signed in to change notification settings

FloppyShelf/Problemize

Repository files navigation

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
builder.Services.UseExceptionHandling();
  1. Configure middleware in Program.cs
app.UseExceptionHandling();

Sample ProblemDetails response

{
  "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..."
  }
}

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages