Persilsoft.Exceptions 1.0.0

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

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

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");

Puede ver un ejemplo de ValidationException en la documentaci�n del paquete 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 321 10/16/2024
1.0.10 1,247 7/20/2024
1.0.9 1,117 5/25/2024
1.0.8 214 5/22/2024
1.0.7 224 5/21/2024
1.0.6 701 5/4/2024
1.0.5 285 4/28/2024
1.0.4 117 4/28/2024
1.0.3 399 4/27/2024
1.0.2 373 4/14/2024
1.0.1 124 4/12/2024
1.0.0 107 4/12/2024