Extor 1.0.0
dotnet add package Extor --version 1.0.0
NuGet\Install-Package Extor -Version 1.0.0
<PackageReference Include="Extor" Version="1.0.0" />
paket add Extor --version 1.0.0
#r "nuget: Extor, 1.0.0"
// Install Extor as a Cake Addin #addin nuget:?package=Extor&version=1.0.0 // Install Extor as a Cake Tool #tool nuget:?package=Extor&version=1.0.0
Extor π
Extor is a powerful and flexible global exception handling library for .NET applications. It provides a fluent interface for configuring exception handling, complete with method chaining and customizable middleware. Whether you're building a small app or a large enterprise system, Extor is here to manage your exceptions gracefully.
Features β¨
- Global Exception Handling: Handle all exceptions in a consistent manner across your application.
- Fluent API: Configure exception handling with a clean and readable API.
- Customizable Middleware: Easily plug Extor into your middleware pipeline.
- Service Registration: Seamlessly integrate with the .NET dependency injection system.
- .NET Core 6.0+
Installation π οΈ
To install Extor, simply add the NuGet package to your project or use bash command:
dotnet add package Extor
Usage π―
Register Extor Services
builder.Services.AddExtor(typeof(BadRequestException));
Registering exception assemblies is optional (You must send method parameterless to do this). It is required if you intend to use the WithName method; otherwise, the WithName method will default to the Exception
type.
[!NOTE] For same assembly members, only one
typeof(Exception)
is sufficient. The Method can take moretypeof(Exception)
if you have different assemblies.
Adding Extor middleware
app.UseExtor()
.Handle(typeof(BadRequestException), 400, "GLOBAL_MESSAGE", true)
.Handle(typeof(NullException), HttpStatusCode.BadRequest, "NULL_EXCEPTION", false);
The Handle
methods are optional. Since you can't alter Extor middleware, you can handle specific exception classes here.
[!NOTE] The
Handle
method takes parametertypeof(Exception)
,int
/HttpStatusCode
,globalMessage
andmessageOverriding
typeof(Exception)
: Determines which class will be handled.int
/HttpStatusCode
: Determines the status code of the message. To see behavior withbuilder
methods, click hereglobalMessage
: Sets global message for sepcific class.messageOverriding
: Enables/disables global message overriding.
To see both globalMessage
and messageOverriding
behaviors with builder
methods, click here
[!TIP] The
app.UseExtor
method handles exceptions not only from IExtor but also from catch blocks, regular exception throwing, and system exceptions.
Example π¬
public class ExampleController : ControllerBase
{
private readonly IExtor _extor;
public ExampleController(IExtor extor)
{
_extor = extor;
}
[HttpPost]
public IActionResult Get()
{
if(true)
{
_extor
.Create()
.WithType(new TestException())
.WithMessage("Test-case")
.WithStatusCode(400)
.Build()
.Throw();
}
return Ok("Success!");
}
}
Method Overview π
Create()
Creates a new exception builder. If no builder methods are used, then anException
will be created with default values.WithName(string name)
Sets the name of the exception. Assembly must be registered here ifException
class is created by userWithType(Exception exception)
Sets the type of the exception. Assembly registration is not needed. Exception message can be passed withconstructor
[!NOTE] WithMessage method always ignores the
constructor
.
[!CAUTION]
WithName
andWithType
cannot be used at the same time.
WithStatusCode(int statusCode) / WithStatusCode(HttpStatusCode statusCode)
Sets the status code of the exception. Paramater can be passed as anint
or enum fromSystem.Net.HttpStatusCode
. Default value is500
, can be overwritten in the Handle.
[!NOTE]
WithStatusCode
method always ignores Handle's StatusCode.
WithMessage(string message)
Sets the message of the exception.
[!NOTE] either
constructor
orWithMessage
will be ignored if messageOverriding is enabled.
[!WARNING] If messageOverrading is disabled and globalMessage is provided, and if neither WithMessage nor
constructor
are used for the message then globalMessage will be triggered.
Build()
Builds the exception.Throw / ThrowIf(bool predicate)
Throws the built exception. TheThrow
method throws exception regardless of conditions. Itβs better used with If-else statements or try-catch blocks. However, TheThrowIf
method throws exception only if the condition istrue
[!TIP] More detailed explanations and behaviors of each method, including combinations and global handler methods, can be found in the TestController
Product | Versions 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. |
-
net6.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
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.0 | 112 | 8/19/2024 |