Sstv.DomainExceptions.Extensions.ProblemDetails 3.1.0

dotnet add package Sstv.DomainExceptions.Extensions.ProblemDetails --version 3.1.0
                    
NuGet\Install-Package Sstv.DomainExceptions.Extensions.ProblemDetails -Version 3.1.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="Sstv.DomainExceptions.Extensions.ProblemDetails" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sstv.DomainExceptions.Extensions.ProblemDetails" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Sstv.DomainExceptions.Extensions.ProblemDetails" />
                    
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 Sstv.DomainExceptions.Extensions.ProblemDetails --version 3.1.0
                    
#r "nuget: Sstv.DomainExceptions.Extensions.ProblemDetails, 3.1.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.
#addin nuget:?package=Sstv.DomainExceptions.Extensions.ProblemDetails&version=3.1.0
                    
Install Sstv.DomainExceptions.Extensions.ProblemDetails as a Cake Addin
#tool nuget:?package=Sstv.DomainExceptions.Extensions.ProblemDetails&version=3.1.0
                    
Install Sstv.DomainExceptions.Extensions.ProblemDetails as a Cake Tool

Sstv.DomainExceptions.Extensions.ProblemDetails

<- root readme

<- changelog

This library brings to Sstv.DomainExceptions additional capabilities to create response in ProblemDetails format

Install

You can install using Nuget Package Manager:

Install-Package Sstv.DomainExceptions.Extensions.ProblemDetails -Version 3.1.0

via the .NET CLI:

dotnet add package Sstv.DomainExceptions.Extensions.ProblemDetails --version 3.1.0

or you can add package reference manually:

<PackageReference Include="Sstv.DomainExceptions.Extensions.ProblemDetails" Version="3.1.0" />

How to use?

Register to Dependency injection:

Call UseDomainExceptionHandler extension method on DomainExceptionBuilder:

services.AddDomainExceptions(builder =>
{
    builder.UseDomainExceptionHandler();

    // other config
});

Add AspNetCore built-in ProblemDetails

services.AddProblemDetails(x =>
{
    x.CustomizeProblemDetails = context =>
    {
        // when domain exception occurs, we can grab error code and map HTTP status code for him
        if (context.Exception is DomainException de)
        {
            context.ProblemDetails.Status = context.HttpContext.Response.StatusCode = ErrorCodeMapping.MapToStatusCode(de.ErrorCode);
        }
        else
        {
            // for other exceptions we can add default error code for consistent behavior
            context.ProblemDetails = new ErrorCodeProblemDetails(ErrorCodes.Default.GetDescription())
            {
                Status = context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError
            };
        }
    };
})

Please, look at here for additional examples.

Add exception handler middlware

app.UseExceptionHandler();

after that, we can throw exception and see results

throw ErrorCodes.NotEnoughMoney.ToException()
                .WithDetailedMessage("You want 500, but your account balance is 300.");
{
    "type": "https://help.myproject.ru/error-codes/not-enough-money",
    "title": "You have not enough money",
    "detail": "You want 500, but your account balance is 300.",
    "status": 200,
    "code": "SSTV10001",
    "criticalityLevel": "Low",
    "errorId": "ad3f064c-1254-41dd-82e4-891507937cf6"
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
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
3.1.0 144 4/2/2025
3.0.0 312 12/5/2024
2.4.0 136 4/2/2025
2.3.0 103 12/6/2024
2.2.0 164 2/18/2024