Persilsoft.Exceptions 1.0.8

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

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

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
    .AddNotFoundExceptionHandler()
    .AddValidationExceptionHandler()
    .AddUpdateExceptionHandler()
    .AddUnhandledExceptionHandler();

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 (5)

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

Package Downloads
Persilsoft.Membership.Abstractions

Contains fundamental classes and interfaces for use in backend membership projects.

Persilsoft.Recaptcha.Server

Exposes an endpoint to interact with the Google API to validate a reCAPTCHA V3 token.

Persilsoft.Sms.Abstractions

Contains an interface to be used in an SMS-sending project

Persilsoft.Culqi.Server

Expone algunos endpoints para interactuar con la API de Culqi durante el proceso de Checkout.

Persilsoft.Mail.MailKit.Sender

An implementation of MailKit (a popular Mail library)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.11 305 10/16/2024
1.0.10 1,247 7/20/2024
1.0.9 1,115 5/25/2024
1.0.8 210 5/22/2024
1.0.7 223 5/21/2024
1.0.6 700 5/4/2024
1.0.5 282 4/28/2024
1.0.4 116 4/28/2024
1.0.3 398 4/27/2024
1.0.2 372 4/14/2024
1.0.1 121 4/12/2024
1.0.0 106 4/12/2024