Extor 1.0.0

dotnet add package Extor --version 1.0.0                
NuGet\Install-Package Extor -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="Extor" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Extor --version 1.0.0                
#r "nuget: Extor, 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 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 🎯

  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

Product 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. 
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.0 105 8/19/2024