Part.AspNetCore.FriendlyExceptions 0.1.2

dotnet add package Part.AspNetCore.FriendlyExceptions --version 0.1.2                
NuGet\Install-Package Part.AspNetCore.FriendlyExceptions -Version 0.1.2                
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="Part.AspNetCore.FriendlyExceptions" Version="0.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Part.AspNetCore.FriendlyExceptions --version 0.1.2                
#r "nuget: Part.AspNetCore.FriendlyExceptions, 0.1.2"                
#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 Part.AspNetCore.FriendlyExceptions as a Cake Addin
#addin nuget:?package=Part.AspNetCore.FriendlyExceptions&version=0.1.2

// Install Part.AspNetCore.FriendlyExceptions as a Cake Tool
#tool nuget:?package=Part.AspNetCore.FriendlyExceptions&version=0.1.2                

ASP.NET Core Friendly Exceptions Filter and Middleware Build Status

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 created by Anders Åberg and has been adapted for ASP.NET Core usage by Andriy S'omak.

Installation

Before using of the library Nuget Package 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.

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddFriendlyExceptionsTransforms(options =>
            {
                options.Transforms = TransformsCollectionBuilder.Begin()
    
                    .Map<ExampleException>()
                    .To(HttpStatusCode.BadRequest, "This is the reasonphrase",
                        ex => "And this is the response content: " + ex.Message)
    
                    .Map<SomeCustomException>()
                    .To(HttpStatusCode.NoContent, "Bucket is emtpy", ex => string.Format("Inner details: {0}", ex.Message))
    
                    .Map<EntityUnknownException>()
                    .To(HttpStatusCode.NotFound, "Entity does not exist", ex => ex.Message)
    
                    .Map<InvalidAuthenticationException>()
                    .To(HttpStatusCode.Unauthorized, "Invalid authentication", ex => ex.Message)
    
                    .Map<AuthorizationException>()
                    .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:

    .Map<SomeJsonException>()
    .To(HttpStatusCode.BadRequest, "This exception is json",
        ex => JsonConvert.SerializeObject(ex.Message), "application/json")

Using filter

Register ExceptionFilter.

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            services.AddMvc()
                .AddMvcOptions(s => s.Filters.Add(typeof(FriendlyExceptionAttribute)))
            ...
        }                

Add filter attribute to the Controller.

    [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.

        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

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.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.2 1,080 5/8/2024

Adeed possibility to use ExceptionFilter without callingTypeFilter()