Package Description
License
—
Deps
2
Install Size
—
Vulns
✓ 0
Published
Apr 2, 2025
$ dotnet add package Sstv.DomainExceptions.Extensions.ProblemDetailsThis library brings to Sstv.DomainExceptions additional capabilities to create response in ProblemDetails format
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" />
Call UseDomainExceptionHandler extension method on DomainExceptionBuilder:
services.AddDomainExceptions(builder =>
{
builder.UseDomainExceptionHandler();
// other config
});
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.
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"
}