A .NET library providing unified error handling for ASP.NET Core applications. The package implements middleware that catches exceptions and transforms them into standardized JSON responses with proper HTTP status codes. Key Features: - Global exception handling middleware - Standardized error response format - Built-in support for common exception types - Request correlation tracking - Configurable stack trace visibility - Consistent logging Designed to streamline error handling across microservices and web applications while maintaining proper error tracking and debugging capabilities.
$ dotnet add package Cross.ErrorHandlersA .NET library providing unified error handling for ASP.NET Core applications. The package implements middleware that catches exceptions and transforms them into standardized JSON responses with proper HTTP status codes.
Designed to streamline error handling across microservices and web applications while maintaining proper error tracking and debugging capabilities.
https://www.nuget.org/packages/Cross.ErrorHandlers
Install via NuGet Package Manager:
dotnet add package Cross.ErrorHandlers
Or via Package Manager Console:
Install-Package Cross.ErrorHandlers
Program.cs or Startup.cs:public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// ...
var app = builder.Build();
// Add error handler middleware (should be first in the pipeline)
app.UseMiddleware<ErrorHandlerMiddleware>();
// Other middleware configurations...
app.Run();
}
}
All errors are returned in a consistent JSON format:
{
"code": "errorCode",
"message": "Error description",
"correlationId": "00000000-0000-0000-0000-000000000000",
"errors": {
"field1": ["Error message 1", "Error message 2"],
"field2": ["Error message 3"]
}
}
The middleware handles various exception types with appropriate HTTP status codes:
| Exception Type | HTTP Status Code | Error Code |
|---|---|---|
| ValidationException | 406 | InvalidParameters |
| JsonException | 406 | InvalidParameters |
| NotFoundException | 404 | NotFound |
| ConflictException | 409 | Conflict |
| BadRequestException | 400 | BadRequest |
| NotAuthorizedException | 401 | NotAuthorized |
| ForbiddenException | 403 | Forbidden |
| HttpClientException | 400 | InvalidClient |
| Exception (default) | 500 | InternalServerError |
Configure stack trace visibility in appsettings.json:
{
"ClearStackTraceErrors": true // Set to false to include stack traces in responses
}
The middleware automatically handles correlation IDs:
X-Correlation-Id header if providedpublic class YourController : ControllerBase
{
[HttpGet]
public IActionResult Get(int id)
{
var item = _repository.GetById(id);
if (item == null)
throw new NotFoundException($"Item with id {id} not found");
return Ok(item);
}
}
Response when item not found:
{
"code": "NotFound",
"message": "Item with id 123 not found",
"correlationId": "7b2ab0e6-3c42-4488-a8b5-9b6d15b63e1a",
"errors": {
"Exception": ["NotFoundException: Item with id 123 not found"]
}
}
Contribution is welcomed. If you would like to provide a PR please add some testing.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.