Skip to content
/ Extor Public

Developer-friendly Global exception handler for web api

License

Notifications You must be signed in to change notification settings

Novruzt/Extor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 🎯

  1. 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 more typeof(Exception) if you have different assemblies.

  1. 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 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 with builder methods, click here
  • globalMessage: 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 an Exception will be created with default values.
  • WithName(string name)

    Sets the name of the exception. Assembly must be registered here if Exception class is created by user
  • WithType(Exception exception)

    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.

  • WithStatusCode(int statusCode) / WithStatusCode(HttpStatusCode statusCode)

    Sets the status code of the exception. Paramater can be passed as an int or enum from System.Net.HttpStatusCode. Default value is 500, 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 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.

  • Build()

    Builds the exception.
  • Throw / ThrowIf(bool predicate)

    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 is true

Tip

More detailed explanations and behaviors of each method, including combinations and global handler methods, can be found in the TestController

About

Developer-friendly Global exception handler for web api

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages