Sstv.DomainExceptions.Extensions.ProblemDetails
2.2.0
dotnet add package Sstv.DomainExceptions.Extensions.ProblemDetails --version 2.2.0
NuGet\Install-Package Sstv.DomainExceptions.Extensions.ProblemDetails -Version 2.2.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="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sstv.DomainExceptions.Extensions.ProblemDetails --version 2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sstv.DomainExceptions.Extensions.ProblemDetails, 2.2.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.
// Install Sstv.DomainExceptions.Extensions.ProblemDetails as a Cake Addin #addin nuget:?package=Sstv.DomainExceptions.Extensions.ProblemDetails&version=2.2.0 // Install Sstv.DomainExceptions.Extensions.ProblemDetails as a Cake Tool #tool nuget:?package=Sstv.DomainExceptions.Extensions.ProblemDetails&version=2.2.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 2.2.0
via the .NET CLI:
dotnet add package Sstv.DomainExceptions.Extensions.ProblemDetails --version 2.2.0
or you can add package reference manually:
<PackageReference Include="Sstv.DomainExceptions.Extensions.ProblemDetails" Version="2.2.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
};
}
};
})
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",
"status": 200,
"code": "SSTV10001",
"errorId": "ad3f064c-1254-41dd-82e4-891507937cf6"
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
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 |
---|---|---|
2.2.0 | 145 | 2/18/2024 |