ExceptionToProblemDetails 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ExceptionToProblemDetails --version 1.1.0
NuGet\Install-Package ExceptionToProblemDetails -Version 1.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="ExceptionToProblemDetails" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExceptionToProblemDetails --version 1.1.0
#r "nuget: ExceptionToProblemDetails, 1.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.
// Install ExceptionToProblemDetails as a Cake Addin
#addin nuget:?package=ExceptionToProblemDetails&version=1.1.0

// Install ExceptionToProblemDetails as a Cake Tool
#tool nuget:?package=ExceptionToProblemDetails&version=1.1.0

In English - bellow

Motyvacija

Susieti OPEN API dokumentaciją su standartizuotų klaidų apdorojimu ProblemDetails.

Pastaba - turi būti kartu naudojamas su kokiu nors ProblemDetails paketu, pvz.:

Naudojimas

1. Prisidėti paketą package

Install-Package ExceptionToProblemDetails

.NET Core command line interface:

dotnet add package ExceptionToProblemDetails

2. Prisidėti source code generatoriaus paketą package

Install-Package ExceptionToProblemDetails.Generator

.NET Core command line interface:

dotnet add package ExceptionToProblemDetails.Generator

3. Susikurti partial klasę ExceptionToProblemDetailsMap, kuri užregistruos middleware Exception mapper

Žemiau pateiktas pavyzdys, registruojantis Hellang.Middleware.ProblemDetails middleware

public partial class ExceptionToProblemDetailsMap
{
    private readonly ProblemDetailsOptions options;

    public ExceptionToProblemDetailsMap(ProblemDetailsOptions options)
    {
        this.options = options;
    }

    partial void MapConverter<TConverter, TException, TProblemDetails>(int statusCode, ExceptionToProblemDetails.ControllerActionDefinition actionDefinition) where TConverter : ExceptionToProblemDetails.IExceptionToProblemDetailsConverter<TException, TProblemDetails> where TException : System.Exception where TProblemDetails : Microsoft.AspNetCore.Mvc.ProblemDetails
    {
        var converter = Activator.CreateInstance<TConverter>();
        options.Map<TException>((context, exception) =>
            actionDefinition.MatchRoute(context.Request.RouteValues)
                ? converter.Convert(exception, statusCode)
                : null);
    }
}

4. Dekoruoti controller klasės arba action metodus atributu [MapToProblemDetails]

[HttpGet("{location}")]
[ProducesResponseType(typeof(IEnumerable<WeatherForecast> ),StatusCodes.Status200OK )]
[MapToProblemDetails(StatusCodes.Status404NotFound, ExceptionType = typeof(NotFoundException))]
public IEnumerable<WeatherForecast> Get(string location)
{

Source generatorius sugeneruos 3 žingsnyje apsirašytos partial klasės iškvietimus

5. Naudojant jums patinkantį ProblemDetails middleware užregistruoti Exception mapper

builder.Services.AddProblemDetails(options =>
{
    new ExceptionToProblemDetailsMap(options).Map();
    options.IncludeExceptionDetails = (context, exception) => false;
});

Pavyzdžiai pateikti pavyzdiniai projektai

<a id="Motivation"></a>

Motivation

Tie the Open API documentation to standardized error handling with ProblemDetails.

Note - Must be used along with a ProblemDetails package such as:

Usage

1. Add the package

Install-Package ExceptionToProblemDetails

.NET Core command line interface:

dotnet add package ExceptionToProblemDetails

2. Add the source code generator package

Install-Package ExceptionToProblemDetails.Generator

.NET Core command line interface:

dotnet add package ExceptionToProblemDetails.Generator

3. Create a partial class ExceptionToProblemDetailsMap, which will register the Exception mapper middleware

Below is an example registering the Hellang.Middleware.ProblemDetails middleware.

public partial class ExceptionToProblemDetailsMap
{
    private readonly ProblemDetailsOptions options;

    public ExceptionToProblemDetailsMap(ProblemDetailsOptions options)
    {
        this.options = options;
    }

    partial void MapConverter<TConverter, TException, TProblemDetails>(int statusCode, ExceptionToProblemDetails.ControllerActionDefinition actionDefinition) where TConverter : ExceptionToProblemDetails.IExceptionToProblemDetailsConverter<TException, TProblemDetails> where TException : System.Exception where TProblemDetails : Microsoft.AspNetCore.Mvc.ProblemDetails
    {
        var converter = Activator.CreateInstance<TConverter>();
        options.Map<TException>((context, exception) =>
            actionDefinition.MatchRoute(context.Request.RouteValues)
                ? converter.Convert(exception, statusCode)
                : null);
    }
}

4. Decorate the controller class or action methods with the [MapToProblemDetails] attribute.

[HttpGet("{location}")]
[ProducesResponseType(typeof(IEnumerable<WeatherForecast> ),StatusCodes.Status200OK )]
[MapToProblemDetails(StatusCodes.Status404NotFound, ExceptionType = typeof(NotFoundException))]
public IEnumerable<WeatherForecast> Get(string location)
{

The source generator will generate the calls described in step 3.

5. Using your preferred ProblemDetails middleware, register the Exception mapper.


builder.Services.AddProblemDetails(options =>
{
    new ExceptionToProblemDetailsMap(options).Map();
    options.IncludeExceptionDetails = (context, exception) => false;
});

Examples can be found in the sample project

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.4.3 828 5/20/2023
1.4.0 396 5/10/2023
1.3.0 194 3/26/2023
1.2.0 351 2/21/2023
1.1.0 267 1/30/2023
1.0.0 397 1/29/2023