Api.Http.Extensions.Filters.ExceptionHandler 1.0.0

dotnet add package Api.Http.Extensions.Filters.ExceptionHandler --version 1.0.0                
NuGet\Install-Package Api.Http.Extensions.Filters.ExceptionHandler -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="Api.Http.Extensions.Filters.ExceptionHandler" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Api.Http.Extensions.Filters.ExceptionHandler --version 1.0.0                
#r "nuget: Api.Http.Extensions.Filters.ExceptionHandler, 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 Api.Http.Extensions.Filters.ExceptionHandler as a Cake Addin
#addin nuget:?package=Api.Http.Extensions.Filters.ExceptionHandler&version=1.0.0

// Install Api.Http.Extensions.Filters.ExceptionHandler as a Cake Tool
#tool nuget:?package=Api.Http.Extensions.Filters.ExceptionHandler&version=1.0.0                

api-http-extensions

Content


About project

Name

Api.Http.Extensions.Filters.ExceptionHandler

Description

This library was development to give to response a standard format when occurred a Technical error and control the functional errors o request validations en the API Requests. The implementation is simple, install the package and add services in Startup.cs.

Badges

.Net Visual Studio C#

Installation

Usage

Mandatory Parameters_Missing (Http Code 400)

Add services into Setup.cs

Add AddValitationFilter() and AddHandlerExceptionFilter() into ConfigureServices method in Startup.cs class.

using Api.Http.Extensions.Filters.ExceptionHandler;

public void ConfigureServices(IServiceCollection services)
{
	services.AddValitationFilter();
 	services.AddHandlerExceptionFilter();
}
Validator Implementation to mandatory fields

To the fields validation add FluentValidation package, and create the class validator for each request, for example:

public class CreateDummyRequestValidator : AbstractValidator<CreateDummyRequest>
{
    public CreateDummyRequestValidator()
    {
        RuleFor(x => x.Name).NotEmpty().WithMessage("The name is required");
        RuleFor(x => x.LastName).NotNull().NotEmpty().WithMessage("The last name is required");
    }
}

After add the validator class in ConfigureServices into Startup.cs, example:

using Api.Http.Extensions.Filters.ExceptionHandler;

public void ConfigureServices(IServiceCollection services)
{
	services.AddValitationFilter();
 	services.AddHandlerExceptionFilter();
	services.AddTransient<IValidator<CreateDummyRequest>, CreateDummyRequestValidator>();
}
Filters implementation in controller

Add in Controller the next code:

using Api.Http.Extensions.Filters.ExceptionHandler.Filters;

[TypeFilter(typeof(RequestValidatorFilter))]
[Route("api/[controller]")]
[ApiController]
public class DummyController : ControllerBase
{
	public IActionResult Post([FromBody] CreateDummyRequest request)
	{
    	return _dummyCreateUseCase.Execute(request);
	}
}
Execute validation

Request:

{
  "name": "",
  "lastName": ""
}

Response:

{
  "alias": "MANDATORY_PARAMETERS_MISSING",
  "code": 400,
  "message": [
    {
      "name": "Name",
      "message": "The name is required"
    },
    {
      "name": "LastName",
      "message": "The last name is required"
    }
  ],
  "type": "WARNING"
}

Functional Error (Http Code 400)

Add services into Setup.cs

Add AddValitationFilter() and AddHandlerExceptionFilter() into ConfigureServices method in Startup.cs class.

using Api.Http.Extensions.Filters.ExceptionHandler;

public void ConfigureServices(IServiceCollection services)
{
	services.AddValitationFilter();
 	services.AddHandlerExceptionFilter();
}
Add throw FunctionalException

Add throw FunctionalException en the Services.

public void Execute(CreateDummyRequest request)
{
	var findUser = _userRepository.findById(request.id);
	if(findUser is null)
 	{
    	throw new FunctionalException("User is invalid!");
 	}
}
Execute validation
{
	"alias":"FUNCTIONAL_ERROR",
	"code":400,
	"message":"User is invalid!",
	"type":"WARNING"
}

Technical Error (Http Code 500)

Add into try catch the TechnicalException to response to consumer.

public void Execute(CreateDummyRequest request)
{
	try
	{
		var findUser = _userRepository.findById(request.id);
	}
	catch(Exception e)
	{
		throw new TechnicalException(e.Message);
	}
}
Execute response
{
	"alias":"TECHNICAL_ERROR",
	"code":500,
	"message":"A technical error has occurred",
	"type":"ERROR"
}
Unknow Error (Http Code 500)

Add into try catch the UnknowException to response to consumer.

public void Execute(CreateDummyRequest request)
{
	try
	{
		var findUser = _userRepository.findById(request.id);
	}
	catch(Exception e)
	{
		throw new UnknowException(e.Message);
	}
}
{
  "alias": "UNKNOW_ERROR",
  "code": 500,
  "message": "An unknown error has occurred",
  "type": "ERROR"
}

Contact

Name

Bryan Silverio Nieves

Email

bryansilverio12@gmail.com

WebSite

bryansilverio.com

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 133 1/19/2024