TechBuddy.Middlewares.ExceptionHandling
1.0.2
dotnet add package TechBuddy.Middlewares.ExceptionHandling --version 1.0.2
NuGet\Install-Package TechBuddy.Middlewares.ExceptionHandling -Version 1.0.2
<PackageReference Include="TechBuddy.Middlewares.ExceptionHandling" Version="1.0.2" />
paket add TechBuddy.Middlewares.ExceptionHandling --version 1.0.2
#r "nuget: TechBuddy.Middlewares.ExceptionHandling, 1.0.2"
// Install TechBuddy.Middlewares.ExceptionHandling as a Cake Addin #addin nuget:?package=TechBuddy.Middlewares.ExceptionHandling&version=1.0.2 // Install TechBuddy.Middlewares.ExceptionHandling as a Cake Tool #tool nuget:?package=TechBuddy.Middlewares.ExceptionHandling&version=1.0.2
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
- As it will serve infrastructure for web apis, it requires Microsoft.AspNetCore.Http.Abstractions package
- This project has been developing in NET5
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 | Versions 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. |
-
net5.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.