GlobalExceptionHandler 1.0.3

Global Exception Handling Middleware

Global Exception Handler is middleware allowing you to handle exceptions by convention

Install-Package GlobalExceptionHandler -Version 1.0.3
dotnet add package GlobalExceptionHandler --version 1.0.3
paket add GlobalExceptionHandler --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Global Exception Handling for ASP.NET Core

Build status

GlobalExceptionHandlerDotNet allows you to configure exceptions handling as a convention as opposed to explicitly within each controller action. This could be particularly helpful in the following circumstances:

  • Reduce boiler plate try-catch logic in your controllers
  • Catch and appropriately handle exceptions outside of the MVC/WebAPI framework
  • You don't want error codes being visible by consuming APIs (return 500 for every exception)

This middleware currently supports WebAPI with MVC support in the works.

Installation

GlobalExceptionHandler is available on NuGet and can be installed via the below commands depending on your platform:

$ Install-Package GlobalExceptionHandler

or via the .NET Core CLI:

$ dotnet add package GlobalExceptionHandler

Web API Setup

Within your Startup.cs file's Configure method (be sure to call before UseMvc()):

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseWebApiGlobalExceptionHandler(x =>
        {
            x.ForException<PageNotFoundException>().ReturnStatusCode(HttpStatusCode.NotFound);
        });

        app.UseMvc();
    }
}

Returns the following default exception message:

{
    "error": {
        "exception": "PageNotFoundException",
        "message": "Page could not be found"
    }
}

This exception message can be overridden via the ExceptionFormatter method like so:


app.UseWebApiGlobalExceptionHandler(x =>
{
    x.ForException<PageNotFoundException>().ReturnStatusCode(HttpStatusCode.NotFound);
    x.MessageFormatter(exception => JsonConvert.SerializeObject(new
    {
        error = new
        {
            exception = exception.GetType().Name,
            message = exception.Message
        }
    }));
});

Alternatively you can set the formatter to be unique per exception registered. This will override the root x.MessageFormatter referenced above.

app.UseWebApiGlobalExceptionHandler(x =>
{
    x.ForException<ArgumentException>().ReturnStatusCode(HttpStatusCode.BadRequest).UsingMessageFormatter(
        exception => JsonConvert.SerializeObject(new
        {
            error = new
            {
                message = "Oops, something went wrong"
            }
        }));
    x.MessageFormatter(exception => "This formatter will be overridden when an ArgumentException is thrown");
});

Configuration Options:

  • ContentType - Specify the returned content type (default is application/json).

  • MessageFormatter(Func&lt;Exception, string&gt;) - Overrides default JSON message formatter; this is useful if you want to change the error response format or type (XML for instance).

x.MessageFormatter((exception) => {
    return "Oops, something went wrong! Check the logs for more information.";
});

MVC Setup

Work in progress.

Global Exception Handling for ASP.NET Core

Build status

GlobalExceptionHandlerDotNet allows you to configure exceptions handling as a convention as opposed to explicitly within each controller action. This could be particularly helpful in the following circumstances:

  • Reduce boiler plate try-catch logic in your controllers
  • Catch and appropriately handle exceptions outside of the MVC/WebAPI framework
  • You don't want error codes being visible by consuming APIs (return 500 for every exception)

This middleware currently supports WebAPI with MVC support in the works.

Installation

GlobalExceptionHandler is available on NuGet and can be installed via the below commands depending on your platform:

$ Install-Package GlobalExceptionHandler

or via the .NET Core CLI:

$ dotnet add package GlobalExceptionHandler

Web API Setup

Within your Startup.cs file's Configure method (be sure to call before UseMvc()):

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseWebApiGlobalExceptionHandler(x =>
        {
            x.ForException<PageNotFoundException>().ReturnStatusCode(HttpStatusCode.NotFound);
        });

        app.UseMvc();
    }
}

Returns the following default exception message:

{
    "error": {
        "exception": "PageNotFoundException",
        "message": "Page could not be found"
    }
}

This exception message can be overridden via the ExceptionFormatter method like so:


app.UseWebApiGlobalExceptionHandler(x =>
{
    x.ForException<PageNotFoundException>().ReturnStatusCode(HttpStatusCode.NotFound);
    x.MessageFormatter(exception => JsonConvert.SerializeObject(new
    {
        error = new
        {
            exception = exception.GetType().Name,
            message = exception.Message
        }
    }));
});

Alternatively you can set the formatter to be unique per exception registered. This will override the root x.MessageFormatter referenced above.

app.UseWebApiGlobalExceptionHandler(x =>
{
    x.ForException<ArgumentException>().ReturnStatusCode(HttpStatusCode.BadRequest).UsingMessageFormatter(
        exception => JsonConvert.SerializeObject(new
        {
            error = new
            {
                message = "Oops, something went wrong"
            }
        }));
    x.MessageFormatter(exception => "This formatter will be overridden when an ArgumentException is thrown");
});

Configuration Options:

  • ContentType - Specify the returned content type (default is application/json).

  • MessageFormatter(Func&lt;Exception, string&gt;) - Overrides default JSON message formatter; this is useful if you want to change the error response format or type (XML for instance).

x.MessageFormatter((exception) => {
    return "Oops, something went wrong! Check the logs for more information.";
});

MVC Setup

Work in progress.

Release Notes

Bug fixes, add logging endpoint

Version History

Version Downloads Last updated
1.0.3 (current version) 530 10/9/2017
1.0.2 176 9/14/2017
1.0.1 38 9/14/2017
1.0.0 31 9/14/2017
1.0.0-beta 42 9/13/2017