Persilsoft.Exceptions
1.0.4
See the version list below for details.
dotnet add package Persilsoft.Exceptions --version 1.0.4
NuGet\Install-Package Persilsoft.Exceptions -Version 1.0.4
<PackageReference Include="Persilsoft.Exceptions" Version="1.0.4" />
paket add Persilsoft.Exceptions --version 1.0.4
#r "nuget: Persilsoft.Exceptions, 1.0.4"
// Install Persilsoft.Exceptions as a Cake Addin #addin nuget:?package=Persilsoft.Exceptions&version=1.0.4 // Install Persilsoft.Exceptions as a Cake Tool #tool nuget:?package=Persilsoft.Exceptions&version=1.0.4
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 | 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. |
-
net8.0
- Persilsoft.Localizer (>= 1.0.3)
- Persilsoft.Validation (>= 1.0.1)
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.