Cross.ErrorHandlers
7.3.0
dotnet add package Cross.ErrorHandlers --version 7.3.0
NuGet\Install-Package Cross.ErrorHandlers -Version 7.3.0
<PackageReference Include="Cross.ErrorHandlers" Version="7.3.0" />
<PackageVersion Include="Cross.ErrorHandlers" Version="7.3.0" />
<PackageReference Include="Cross.ErrorHandlers" />
paket add Cross.ErrorHandlers --version 7.3.0
#r "nuget: Cross.ErrorHandlers, 7.3.0"
#:package Cross.ErrorHandlers@7.3.0
#addin nuget:?package=Cross.ErrorHandlers&version=7.3.0
#tool nuget:?package=Cross.ErrorHandlers&version=7.3.0
Cross.ErrorHandlers
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
Purpose
Designed to streamline error handling across microservices and web applications while maintaining proper error tracking and debugging capabilities.
Install with nuget.org:
https://www.nuget.org/packages/Cross.ErrorHandlers
Installation
Install via NuGet Package Manager:
dotnet add package Cross.ErrorHandlers
Or via Package Manager Console:
Install-Package Cross.ErrorHandlers
Quick Start
- Register the middleware in your
Program.cs
orStartup.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();
}
}
- The middleware will automatically catch exceptions and return standardized responses.
Error Response Format
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"]
}
}
Supported Exception Types
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 |
Configuration
Configure stack trace visibility in appsettings.json
:
{
"ClearStackTraceErrors": true // Set to false to include stack traces in responses
}
Correlation ID Tracking
The middleware automatically handles correlation IDs:
- Reads from
X-Correlation-Id
header if provided - Generates new GUID if not present
- Includes in error responses for request tracking
Custom Exception Example
public 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"]
}
}
Issues and Pull Request
Contribution is welcomed. If you would like to provide a PR please add some testing.
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Roadmap
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
- FluentValidation (>= 11.8.0)
- MediatR.Contracts (>= 2.0.1)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
7.3.0 | 143 | 6/25/2025 |
7.3.0-preview.16 | 111 | 6/25/2025 |
7.3.0-preview.14 | 114 | 6/25/2025 |
7.3.0-preview.12 | 108 | 6/25/2025 |
7.3.0-preview.10 | 108 | 6/25/2025 |
7.3.0-preview.4 | 120 | 6/25/2025 |
7.2.0 | 11,235 | 7/24/2024 |
7.2.0-preview.4 | 68 | 7/24/2024 |
7.2.0-preview.3 | 69 | 7/24/2024 |
7.1.2 | 20,418 | 5/21/2024 |
7.1.2-preview.4 | 77 | 5/21/2024 |
7.1.2-preview.3 | 79 | 5/21/2024 |
7.1.2-preview.1 | 72 | 5/21/2024 |
7.1.1 | 231 | 5/21/2024 |
7.1.1-preview.2 | 64 | 5/21/2024 |
7.1.1-preview.1 | 65 | 5/21/2024 |
7.1.0 | 131 | 5/20/2024 |
7.1.0-preview.23 | 73 | 5/20/2024 |
7.0.2 | 118 | 5/18/2024 |
7.0.2-preview.16 | 67 | 5/18/2024 |
7.0.1 | 3,027 | 5/3/2024 |
7.0.0 | 105 | 5/3/2024 |
Initial version.