Cross.ErrorHandlers 7.3.0

dotnet add package Cross.ErrorHandlers --version 7.3.0
                    
NuGet\Install-Package Cross.ErrorHandlers -Version 7.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cross.ErrorHandlers" Version="7.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cross.ErrorHandlers" Version="7.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Cross.ErrorHandlers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cross.ErrorHandlers --version 7.3.0
                    
#r "nuget: Cross.ErrorHandlers, 7.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Cross.ErrorHandlers@7.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Cross.ErrorHandlers&version=7.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Cross.ErrorHandlers&version=7.3.0
                    
Install as a Cake Tool

Nuget Documentation

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

  1. Register the middleware in your 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();
    }
}
  1. 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:

  1. Reads from X-Correlation-Id header if provided
  2. Generates new GUID if not present
  3. 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

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Roadmap

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.