Persilsoft.Exceptions 1.0.5

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

// Install Persilsoft.Exceptions as a Cake Tool
#tool nuget:?package=Persilsoft.Exceptions&version=1.0.5

It contains custom exceptions with their respective handlers. These handlers implement the new AspNet Core 8.0 interface IExceptionHandler, formatting the output into a ProblemDetails object serialized to Json.


The package contains the following exceptions:

  • ForbiddenAccessException
  • NotFoundException
  • UpdateException
  • ValidationException

The respective handlers for each of the exceptions are registered as follows:

builder.Services.AddForbiddenExceptionHandler();
builder.Services.AddNotFoundExceptionHandler();
builder.Services.AddUpdateExceptionHandler();
builder.Services.AddValidationExceptionHandler();

The title of the ProblemDetails can be displayed in either English or Spanish. To achieve this, you need to configure localization middleware in your AspNet Core application:

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var SupportedCultures = new[] { "en-US", "es-PE" };
    var NeutralCulture = SupportedCultures[0];

    options.SetDefaultCulture(NeutralCulture)
    .AddSupportedCultures(SupportedCultures)
    .AddSupportedUICultures(SupportedCultures);
});

Finally, we add the localization and exception handling middlewares to the AspNet Core pipeline

app.UseRequestLocalization();
app.UseExceptionHandler(e => { });

Note:
It's important that the localization middleware is added before the exception handling middleware; otherwise, you won't see the messages in the appropriate language.

Example

app.MapGet("/admin/dashboard", () =>
{
    throw new ForbiddenAccessException();
})
.WithName("Admin");

app.MapGet("/product/{id:int}", async (int id, MyContext context) =>
{
    var Product = await context.Products.SingleOrDefaultAsync(p => p.Id == id);
    if (Product is null)
    {
        throw new NotFoundException();
    }

    return Results.Ok(Product);
})
.WithTags("Product");

app.MapPost("/Product/register", async (MyContext context) =>
{
    context.Add(new Product
    {
        Id = 7,
        Name = "Chai",
        UnitPrice = 59.99M,
        UnitsInStock = 80
    });

    try
    {
        await context.SaveChangesAsync();
    }
    catch (DbUpdateException ex)
    {
        throw new UpdateException(ex, ex.Entries.Select(e => e.Entity.GetType().Name));
    }

    return Results.Ok("Product registered.");
})
.WithTags("Product");

You can see an example of ValidationException in the package documentation Persilsoft.Validation

Product 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.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Persilsoft.Exceptions:

Package Downloads
Persilsoft.Membership.Abstractions

Package Description

Persilsoft.Recaptcha.Server

Package Description

Persilsoft.Culqi.Server

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 307 5/4/2024
1.0.5 195 4/28/2024
1.0.4 83 4/28/2024
1.0.3 275 4/27/2024
1.0.2 278 4/14/2024
1.0.1 89 4/12/2024
1.0.0 75 4/12/2024