https://github.com/technobre/powerutils.aspnetcore.errorhandler
Handler to standardize error responses
https://github.com/technobre/powerutils.aspnetcore.errorhandler
aspnetcore csharp dotnet error-handling
Last synced: about 1 year ago
JSON representation
Handler to standardize error responses
- Host: GitHub
- URL: https://github.com/technobre/powerutils.aspnetcore.errorhandler
- Owner: TechNobre
- License: mit
- Created: 2022-02-21T23:07:20.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-11T00:22:07.000Z (about 1 year ago)
- Last Synced: 2025-04-30T15:47:57.333Z (about 1 year ago)
- Topics: aspnetcore, csharp, dotnet, error-handling
- Language: C#
- Homepage: https://www.nuget.org/packages/PowerUtils.AspNetCore.ErrorHandler
- Size: 3.59 MB
- Stars: 6
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# PowerUtils.AspNetCore.ErrorHandler

***Handler to standardize error responses***

[](https://dashboard.stryker-mutator.io/reports/github.com/TechNobre/PowerUtils.AspNetCore.ErrorHandler/main)
[](https://sonarcloud.io/summary/new_code?id=TechNobre_PowerUtils.AspNetCore.ErrorHandler)
[](https://sonarcloud.io/summary/new_code?id=TechNobre_PowerUtils.AspNetCore.ErrorHandler)
[](https://sonarcloud.io/summary/new_code?id=TechNobre_PowerUtils.AspNetCore.ErrorHandler)
[](https://sonarcloud.io/summary/new_code?id=TechNobre_PowerUtils.AspNetCore.ErrorHandler)
[](https://www.nuget.org/packages/PowerUtils.AspNetCore.ErrorHandler)
[](https://www.nuget.org/packages/PowerUtils.AspNetCore.ErrorHandler)
[](https://github.com/TechNobre/PowerUtils.AspNetCore.ErrorHandler/blob/main/LICENSE)
- [Support to](#support-to)
- [Dependencies](#dependencies)
- [How to use](#how-to-use)
- [Install NuGet package](#install-nuget-package)
- [Configure](#configure)
- [PropertyNamingPolicy](#propertynamingpolicy)
- [ExceptionMappers](#exceptionmappers)
- [IProblemFactory](#iproblemfactory)
- [Customize problem link and problem title](#customize-problem-link-and-problem-title)
- [Add new custom status code](#add-new-custom-status-code)
- [Change link and title for a specific status code](#change-link-and-title-for-a-specific-status-code)
- [Contribution ](#contribution-)
## Support to
- .NET 9.0
- .NET 8.0
- .NET 7.0
- .NET 6.0
- .NET 5.0
- Microsoft.AspNetCore.App [NuGet](https://www.nuget.org/packages/Microsoft.AspNetCore.App/)
### Install NuGet package
This package is available through Nuget Packages: https://www.nuget.org/packages/PowerUtils.AspNetCore.ErrorHandler
**Nuget**
```bash
Install-Package PowerUtils.AspNetCore.ErrorHandler
```
**.NET CLI**
```
dotnet add package PowerUtils.AspNetCore.ErrorHandler
```
ErrorHandler configuration
```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddErrorHandler();
}
public void Configure(IApplicationBuilder app)
{
app.UseErrorHandler();
}
}
```
#### PropertyNamingPolicy
**Options:**
- **Original**: _Do not format the property_;
- **CamelCase**: E.g. from `ClientName` to `clientName` **Default value**;
- **SnakeCase**: E.g. from `ClientName` to `client_name`;
```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddErrorHandler(options =>
{
options.PropertyNamingPolicy = PropertyNamingPolicy.SnakeCase;
});
}
}
```
#### ExceptionMappers
Exception mapping to status code and error codes
```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddErrorHandler(options =>
{
options.ExceptionMapper(exception => StatusCodes.Status409Conflict);
options.ExceptionMapper(exception => (400, exception.Property, exception.Code, exception.Message));
options.ExceptionMapper(exception => (
exception.Status,
exception.Errors.ToDictionary(
k => k.Key,
v => new ErrorDetails(v.Value, exception.Message)
)));
});
}
}
```
#### IProblemFactory
How to create a custom error problem details for example in a controller
```csharp
[ApiController]
[Route("home")]
public class HomeController : ControllerBase
{
private readonly IProblemFactory _problemFactory;
public ProblemFactoryController(IProblemFactory problemFactory)
=> _problemFactory = problemFactory;
[HttpGet("call-1")]
public IActionResult Call1()
=> _problemFactory.CreateProblemResult(
detail: "detail",
instance: "instance",
statusCode: (int)HttpStatusCode.BadRequest,
title: "title",
type: "type",
errors: new Dictionary
{
["Property1"] = new("Error1", "Message1"),
["Property2"] = new("Error2", "Message2"),
["Property3"] = new("Error3", "Message3"),
});
[HttpGet("call-2")]
public IActionResult Call2()
=> new ObjectResult(_problemFactory.CreateProblem(
detail: "detail",
instance: "instance",
statusCode: (int)HttpStatusCode.BadRequest,
title: "title",
type: "type",
errors: new Dictionary
{
["Property1"] = new("Error1", "Message1"),
["Property2"] = new("Error2", "Message2"),
["Property3"] = new("Error3", "Message3"),
}
));
}
```
#### Customize problem link and problem title
Exception mapping to status code and error codes
##### Add new custom status code
```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.Configure(options =>
{
options.ClientErrorMapping.Add(582, new()
{
Link = "CustomLink",
Title = "CustomTitle"
});
});
}
}
```
##### Change link and title for a specific status code
Add your customization after `services.AddErrorHandler();` because it will override the defaults status codes
```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddErrorHandler();
services.Configure(options =>
{
options.ClientErrorMapping[403].Link = "OverrideLink";
options.ClientErrorMapping[403].Title = "OverrideTitle";
});
}
}
```
If you have any questions, comments, or suggestions, please open an [issue](https://github.com/TechNobre/PowerUtils.AspNetCore.ErrorHandler/issues/new/choose) or create a [pull request](https://github.com/TechNobre/PowerUtils.AspNetCore.ErrorHandler/compare)