Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/semack/AspNetCore.FriendlyExceptions
ASP.NET Core Filter and Middleware to catch exceptions and translate them into nice HTTP responses
https://github.com/semack/AspNetCore.FriendlyExceptions
asp-net-core asp-net-core-mvc asp-net-core-web-api exception-handling middleware
Last synced: 2 months ago
JSON representation
ASP.NET Core Filter and Middleware to catch exceptions and translate them into nice HTTP responses
- Host: GitHub
- URL: https://github.com/semack/AspNetCore.FriendlyExceptions
- Owner: semack
- License: mit
- Created: 2017-07-04T20:49:21.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-02T08:54:50.000Z (almost 6 years ago)
- Last Synced: 2024-11-03T16:06:25.437Z (3 months ago)
- Topics: asp-net-core, asp-net-core-mvc, asp-net-core-web-api, exception-handling, middleware
- Language: C#
- Homepage:
- Size: 38.1 KB
- Stars: 17
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-dotnet - AspNetCore.FriendlyExceptions - ASP.NET Core Filter and Middleware to catch exceptions and translate them into nice HTTP responses (Libraries, Frameworks and Tools / Exceptions)
README
# ASP.NET Core Friendly Exceptions Filter and Middleware [![Build Status](https://travis-ci.org/semack/AspNetCore.FriendlyExceptions.svg?branch=master)](https://travis-ci.org/semack/AspNetCore.FriendlyExceptions)
A filter and middleware that can translate exceptions into nice http resonses. This allows you to throw meaningfull exceptions from your framework, business code or other middlewares and translate the exceptions to nice and friendly http responses.
## Authors
This code based on [Owin Friendly Exceptions Middleware](https://github.com/abergs/OwinFriendlyExceptions) created by [Anders Åberg](https://github.com/abergs) and has been adapted for ASP.NET Core usage by [Andriy S'omak](https://github.com/semack).## Installation
Before using of the library [Nuget Package](https://www.nuget.org/packages/AspNetCore.FriendlyExceptions/) must be installed.`Install-Package AspNetCore.FriendlyExceptions`
## Examples of usage
There are a two ways of using of the library: using ExceptionFilter or registering Midlleware. You can choose any of them.### Configuration
Add transformation rules to the Startup.cs file.
```cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddFriendlyExceptionsTransforms(options =>
{
options.Transforms = TransformsCollectionBuilder.Begin()
.Map()
.To(HttpStatusCode.BadRequest, "This is the reasonphrase",
ex => "And this is the response content: " + ex.Message)
.Map()
.To(HttpStatusCode.NoContent, "Bucket is emtpy", ex => string.Format("Inner details: {0}", ex.Message))
.Map()
.To(HttpStatusCode.NotFound, "Entity does not exist", ex => ex.Message)
.Map()
.To(HttpStatusCode.Unauthorized, "Invalid authentication", ex => ex.Message)
.Map()
.To(HttpStatusCode.Forbidden, "Forbidden", ex => ex.Message)
// Map all other exceptions if needed.
// Also it would be useful if you want to map exception to a known model.
.MapAllOthers()
.To(HttpStatusCode.InternalServerError, "Internal Server Error", ex => ex.Message)
.Done();
});
...
}
```By default, FriendlyExceptions sets the response Content-Type to `text/plain`. To use a different type:
```cs
.Map()
.To(HttpStatusCode.BadRequest, "This exception is json",
ex => JsonConvert.SerializeObject(ex.Message), "application/json")
```### Using filter
Register ExceptionFilter.
```cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddMvcOptions(s => s.Filters.Add(typeof(FriendlyExceptionAttribute)))
...
}
```
Add filter attribute to the Controller.
```cs
[Produces("application/json")]
[FriendlyException]
public class AccountController : Controller
{
...
```
### Using Middleware
In case you use middleware, the registration method must be at the top of list of all registrations.
```cs
public void Configure(IApplicationBuilder app)
{
app.UseFriendlyExceptions();
...
}```
## Contribute
Contributions are welcome. Just open an Issue or submit a PR.## Contact
You can reach me via my [email](mailto://[email protected])