Morwinyon.AspNetCore.ExceptionHandling 4.0.0

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

// Install Morwinyon.AspNetCore.ExceptionHandling as a Cake Tool
#tool nuget:?package=Morwinyon.AspNetCore.ExceptionHandling&version=4.0.0                

Exception Handling

Exception Handling is giving you the opportunity to manage all unhandled exceptions in a single point. By implementing this extension in your WebAPI project, you'll be able to catch all unhandled exceptions and return back a standard exception model as well as you'll be able to provide your own handler method which will be called once any unhandled exception is occured.

Moreover, you can provide different handler for different Exception types. For instance, HandlerX for ValidationException or HandlerY for UnAuthorizationException, and HandlerZ for the rest

Apart from that, you can customize logging by providing an ILogger interface to log the details once the exception is cought by the handler. UseExceptionDetails is important because once the default handler is used, it'll return back the exception details. If UseExceptionDetails is true, it returns the StackTrace of the Exception. When it is false, however, it returns the single message ("Internal Server Error Occured!") and 500 StatusCode. So you can use this details in your Dev or PreProd environment but not on production. On the other hand, logging the details is irrelevant to this parameter. No matter it is true or false, it logs everything with StackTrace.

Nuget

Package Name Package Download
ExceptionHandling alternate text is missing from this package README image alternate text is missing from this package README image

Installation

PM> NuGet\Install-Package Morwinyon.AspNetCore.ExceptionHandling

or

dotnet add package Morwinyon.AspNetCore.ExceptionHandling

Usage 1


app.ConfigureMorwinyonExceptionHandling(opt =>
{
    var logger = app.Services.GetService<ILogger>();
    opt.UseExceptionDetails = true; // details will be in the default response model (`DefaultExceptionHandlerResponseModel`)
    opt.UseLogger(logger); // details will be logged not matter `UseExceptionDetails` is true or false

    // If you use logger but provide no logger, it will create its own logger
    // opt.UseLogger();
});

Usage 2

In this usage, you must provide a handler function which will be invoked when the exception is occured. From now on, all the responsibility is on you.

opt.UseCustomHandler((httpContext, exception, logger) => 
{
    logger.LogError("Unhandled exception occured");
    var dynamicResponseModel = new { ErrorMessage = exception.Message };

    // we can set the response but don't have to
    return httpContext.Response.WriteAsJsonAsync(dynamicResponseModel); // WriteAsJsonAsync is an extension method.
});

Usage 3

In this usage, UseExceptionDetails is set to true and also default ILogger is used if it's enabled in the system (app.ApplicationServices.GetService<ILogger>())


app.ConfigureMorwinyonExceptionHandling();

Usage 4

In this usage, you are also able to use different handlers for different type of exceptions.


var options = new ExceptionHandlingOptions();

options.AddCustomHandler<ValidationException>((context, ex, logger) =>
{
    logger.LogError("Unhandled exception occured");
    var dynamicResponseModel = new { ErrorMessage = ex.Message, Type = "ValidationException" };

    // we can set the response but don't have to
    return httpContext.Response.WriteAsJsonAsync(dynamicResponseModel);
});


options.AddCustomHandler<ArgumentNullException>((context, ex, logger) =>
{
    logger.LogError("ArgumentNullException occured");
    var dynamicResponseModel = new { ErrorMessage = ex.Message, Type = "ArgumentNullException" };

    // we can set the response but don't have to
    return httpContext.Response.WriteAsJsonAsync(dynamicResponseModel);
});


// All the other exceptions
opt.UseCustomHandler(async (context, ex, logger) =>
{
    logger.LogError("Unhandled exception occured");
    var dynamicResponseModel = new { ErrorMessage = ex.Message, Type = "Exception" };

    // we can set the response but don't have to
    return httpContext.Response.WriteAsJsonAsync(dynamicResponseModel);
});

app.ConfigureMorwinyonExceptionHandling(options);

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
4.0.0 233 11/28/2023

Summary of changes made in this release of the package.