GlobalExceptionHandler 1.0.3
See the version list below for details.
dotnet add package GlobalExceptionHandler --version 1.0.3
NuGet\Install-Package GlobalExceptionHandler -Version 1.0.3
<PackageReference Include="GlobalExceptionHandler" Version="1.0.3" />
paket add GlobalExceptionHandler --version 1.0.3
#r "nuget: GlobalExceptionHandler, 1.0.3"
// Install GlobalExceptionHandler as a Cake Addin #addin nuget:?package=GlobalExceptionHandler&version=1.0.3 // Install GlobalExceptionHandler as a Cake Tool #tool nuget:?package=GlobalExceptionHandler&version=1.0.3
Global Exception Handling for ASP.NET Core
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 isapplication/json)
.MessageFormatter(Func<Exception, string>)
- 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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http (>= 2.0.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.0.0)
- Newtonsoft.Json (>= 10.0.3)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on GlobalExceptionHandler:
Package | Downloads |
---|---|
GlobalExceptionHandler.ContentNegotiation.Mvc
Enable content negotiation for GlobalExceptionHandlerDotNet when using ASP.NET Core MVC |
|
DiegoRangel.DotNet.Framework.CQRS.API
A common library for implementing CQRS based Api layer. |
|
Prospa.Extensions.AspNetCore.Mvc.Core
ASP.NET Core MVC core components extensions. |
|
Packs.Template.BaseApi
Basis for any Packs API |
|
Harpy.Presentation
Basis for the presentation layer of the Harpy Framework |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.0.2 | 1,814,800 | 3/15/2019 |
4.0.1 | 3,920 | 2/26/2019 |
4.0.0 | 258,043 | 10/27/2018 |
4.0.0-beta2 | 9,287 | 9/20/2018 |
4.0.0-beta1 | 1,846 | 9/18/2018 |
3.0.0 | 67,739 | 12/20/2017 |
2.0.0 | 9,033 | 11/27/2017 |
1.0.3 | 4,973 | 10/9/2017 |
1.0.2 | 1,835 | 9/14/2017 |
1.0.1 | 1,669 | 9/14/2017 |
1.0.0 | 1,837 | 9/14/2017 |
1.0.0-beta | 1,480 | 9/13/2017 |
Bug fixes, add logging endpoint