Owin Middleware to catch exceptions and translate them into nice HTTP responses
C# Batchfile
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Owin Friendly Exceptions Middleware

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


Install-package OwinFriendlyExceptions

See Troubleshooting for help with any installation errors

Plugins for different frameworks

Owin Friendly Exceptions is extensible and can work with frameworks that normally swallows exceptions.

See OwinFriendlyExceptions.Plugins for plugins to handle different frameworks (ASP.NET Web API etc...)


    using System;
    using System.Net;
    using Api.Exceptions;
    using Api.Logic.Exceptions;
    using Owin;
    using OwinFriendlyExceptions;
    using OwinFriendlyExceptions.Extensions;
    using OwinFriendlyExceptions.Plugins.WebApi2;
    namespace Api
        public class Startup
            public void Configuration(IAppBuilder app)
                // Use FriendlyExceptions before your other middlewares
                app.UseFriendlyExceptions(GetFriendlyExceptions(), new [] {new WebApi2ExceptionProvider()});
                // Then the rest of your application
            private ITransformsCollection GetFriendlyExceptions()
                return TransformsCollectionBuilder.Begin()
                    .To(HttpStatusCode.BadRequest, "This is the reasonphrase",
                        ex => "And this is the response content: " + ex.Message)
                    .To(HttpStatusCode.NoContent, "Bucket is emtpy", ex => string.Format("Inner details: {0}", ex.Message))
                    .To(HttpStatusCode.NotFound, "Entity does not exist", ex => ex.Message)
                    .To(HttpStatusCode.Unauthorized, "Invalid authentication", ex => ex.Message)
                    .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.
                    .To(HttpStatusCode.InternalServerError, "Internal Server Error", ex => ex.Message)

Specify content type

By default, OwinFriendlyExceptions sets the response Content-Type to text/plain. To use a different type:

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

Web Api 2 plugin


  1. Install-package OwinFriendlyExceptions.Plugins.WebApi2
  2. app.UseFriendlyExceptions(exceptionsToHandle, new [] {new WebApi2ExceptionProvider()});
  3. config.Services.Replace(typeof(IExceptionHandler), new WebApi2ExceptionHandler(exceptionsToHandle)); Install the package, and supply the WebApi Exception Provider to the OwinFriendlyExceptions extension method. In order for the Plugin to get swalloed wexceptions you have to replace the ExcepionHandler service in Web Api. The plugin takes a list of which exceptions we can handle, so WebApi can still take care of unhandled exceptions for you.


When installing the Web Api Plugin, sometimes your System.Web.Http reference will mismatch. Use this Package Manager Console command to fix your Assembly Bining redirect: Get-Project YourProjectReferencingOwinFriendlyExceptions | Add-BindingRedirect


Contributions are welcome. Just open an Issue or submit a PR.

When publishing nugets:

  • Make sure to set your API key. nuget setApiKey XXX_FROM_NUGET_PORTAL.
  • Open in VS, bump Assembly Version.
  • Build in Release.
  • Run OwinFriendlyExceptions\pack.cmd
  • Run OwinFriendlyExceptions\push.cmd [package-name-path]


You can reach me at @andersaberg or via my blog: ideasof.andersaberg.com