# AspNetCore.FriendlyExceptions **Repository Path**: lanicon/AspNetCore.FriendlyExceptions ## Basic Information - **Project Name**: AspNetCore.FriendlyExceptions - **Description**: ASP.NET Core Filter and Middleware to catch exceptions and translate them into nice HTTP responses - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-09-01 - **Last Updated**: 2024-05-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## 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://semack@gmail.com)