TechBuddy.Extensions.AspNetCore.ExceptionHandling 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package TechBuddy.Extensions.AspNetCore.ExceptionHandling --version 1.0.2                
NuGet\Install-Package TechBuddy.Extensions.AspNetCore.ExceptionHandling -Version 1.0.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="TechBuddy.Extensions.AspNetCore.ExceptionHandling" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TechBuddy.Extensions.AspNetCore.ExceptionHandling --version 1.0.2                
#r "nuget: TechBuddy.Extensions.AspNetCore.ExceptionHandling, 1.0.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 TechBuddy.Extensions.AspNetCore.ExceptionHandling as a Cake Addin
#addin nuget:?package=TechBuddy.Extensions.AspNetCore.ExceptionHandling&version=1.0.2

// Install TechBuddy.Extensions.AspNetCore.ExceptionHandling as a Cake Tool
#tool nuget:?package=TechBuddy.Extensions.AspNetCore.ExceptionHandling&version=1.0.2                

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.

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!") 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.

Usage 1


app.ConfigureTechBuddyExceptionHandling(opt =>
{
    var logger = app.Services.GetService<ILogger>();

    opt.UseLogger(logger, true); // details will be in the default response model (`DefaultExceptionHandlerResponseModel`)
});

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.ConfigureTechBuddyExceptionHandling();

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.ConfigureTechBuddyExceptionHandling(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
1.0.3 474 11/25/2022
1.0.2 320 11/24/2022
1.0.1 313 11/24/2022
1.0.0 320 11/24/2022