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" />
<PackageReference Include="Sstv.DomainExceptions.Extensions.ProblemDetails" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=Sstv.DomainExceptions.Extensions.ProblemDetails&version=3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Sstv.DomainExceptions.Extensions.ProblemDetails
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 | Versions 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.
-
net9.0
- JetBrains.Annotations (>= 2024.3.0)
- Sstv.DomainExceptions.Extensions.DependencyInjection (>= 3.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.