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.
- 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+
To install Extor, simply add the NuGet package to your project or use bash command:
dotnet add package Extor
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 more typeof(Exception)
if you have different assemblies.
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 parameter typeof(Exception)
, int
/ HttpStatusCode
, globalMessage
and messageOverriding
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.
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!");
}
}
-
Creates a new exception builder. If no builder methods are used, then an
Exception
will be created with default values. -
Sets the name of the exception. Assembly must be registered here if
Exception
class is created by user -
Sets the type of the exception. Assembly registration is not needed. Exception message can be passed with
constructor
Note
WithMessage method always ignores the constructor
.
Caution
WithName
and WithType
cannot be used at the same time.
-
Sets the status code of the exception. Paramater can be passed as an
int
or enum fromSystem.Net.HttpStatusCode
. Default value is500
, can be overwritten in the Handle.
Note
WithStatusCode
method always ignores Handle's StatusCode.
- Sets the message of the exception.
Note
either constructor
or WithMessage
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.
- Builds the exception.
-
Throws the built exception.
The
Throw
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