TechBuddy.Middlewares.ExceptionHandling 1.0.0

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

// Install TechBuddy.Middlewares.ExceptionHandling as a Cake Tool
#tool nuget:?package=TechBuddy.Middlewares.ExceptionHandling&version=1.0.0                

ExceptionHandling Middleware

Description

This project represents a build-in ExceptionHandling Middleware as an extension method for IApplicationBuilder It adds middleware for managing and handling unhandled exceptions in your web api project. It is also configurable by passing options whilst initializing.

Dependencies

Getting Started

It can be used parameterless to handle your exceptions and returns a specific response model which includes HttpStatusCode and ExceptionMessage. ExceptionMessage will only contain simple exception message unless it is configured. In this case, the default HttpStatusCode is InternalServerError(500) whereas the default ExceptionMessage is "Internal Server Error Occured!" This extension method can easily be called in Configure method in your startup.cs file of web api projects.

app.AddTBExceptionHandlingMiddleware();
class DefaultResponseModel
{
    public HttpStatusCode HttpStatusCode { get; set; }

    public string ExceptionMessage { get; set; }
}

It can also be used with MiddlewareOptions parameters which allows you to configure your exception handling ability.

ExceptionMessage will be detailed by using exception stack trace string when IsDevelopment is true.

The parameters below will override their original value when they are set.

  • DefaultHttpStatusCode
  • ContentType
  • DefaultMessage

When ExceptionHandlerAction is set, this action will be invoked when any exception has occured.

If you want to override the DefaultResponseModel when an exception has occured, you able to use UseResponseModelCreator method by passing your customized class derived by IResponseModelCreator interface

app.AddTBExceptionHandlingMiddleware(opt =>
{
    opt.IsDevelopment = true;
    opt.DefaultHttpStatusCode = HttpStatusCode.NotFound;
    opt.ContentType = "application/json";
    opt.DefaultMessage = "An Exception Occured";
    opt.ExceptionTypeList.Add<ArgumentNullException>();

    opt.ExceptionHandlerAction = async (httpContext, exception) => 
    {
        // When exception is handled
        var logger = httpContext.RequestServices.GetRequiredService<ILogger>();
        logger.LogError(exception, exception.Message);
    };

    // Use custom respone model
    opt.UseResponseModelCreator<CustomResponseModelCreator>();
});

An example of creating your CustomResponseModelCreator

class CustomResponseModelCreator : IResponseModelCreator
{
    public object CreateModel(ModelCreatorContext model)
    {
        return new DefaultResponseModel()
        {
            ExceptionMessage = model.ErrorMessage,
            HttpStatusCode = model.HttpStatusCode
        };
    }
}

Definition of ModelCreatorContext

public class ModelCreatorContext
{
    public string ErrorMessage { get; set; }

    public HttpStatusCode HttpStatusCode { get; set; }

    public Exception Exception { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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. 
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.2 1,830 1/9/2022
1.0.1 264 1/9/2022
1.0.0 270 1/9/2022