EasyResult is a lightweight library that could be used in your API projects and can handle exceptions
You do not have to pass Result object throughout your whole project, This library is a wrapper on your endpoints
and convert your object into Result object for both success and error scenarios.
You can install EasyResult with NuGet
Install-Package EasyResult
This library gives you two options for handling error scenarios:
1. Returning ActionResult methods
2. Throwing Exceptions
Every exception type has only one HttpStatusCode and you have to register it in your code so everytime that exception throws, API knows which HttpStatusCode should return
You need to register easy result service as below in ConfigureService method:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddEasyResult();
}
You can also set ResultOption:
public void ConfigureServices(IServiceCollection services)
{
builder.Services.AddControllers()
.AddEasyResult(option =>
{
option.SuccessDefaultMessage = "My success default message!";
option.UnhandledExceptionStatusCode = HttpStatusCode.BadGateway;
});
}
for handling exceptions you have to use exception middleware in Configure method:
public void Configure(IApplicationBuilder app)
{
app.UseExceptionMiddleware();
}
There are two ways to register exceptions in EasyResult:
1. Use IExceptionResult interface (this way can only be used for your customize exceptions).
You have to inherit your customize exception from this generic interface and implement its method and assign your desireable HttpStatusCode
public class BadRequestException : Exception, IExceptionResult<BadRequestException>
{
public BadRequestException()
{ }
public BadRequestException(string message)
: base(message)
{ }
public BadRequestException(string message, Exception innerException)
: base(message, innerException)
{ }
public void Configure(ExceptionResultBuilder<BadRequestException> builder)
{
builder.WithHttpStatusCode(HttpStatusCode.BadRequest);
}
}
- Use ExceptionService
In order to use this way, you have to write a middleware as code below (You have to use this way if you want to register built-in exceptions):
public static void AddExceptions(this IApplicationBuilder app)
{
using var scope = app.ApplicationServices.GetService<IServiceScopeFactory>()!.CreateScope();
var exceptionService = scope.ServiceProvider.GetRequiredService<ExceptionService>();
exceptionService.Add(typeof(ApplicationException), HttpStatusCode.Conflict);
exceptionService.Add(typeof(UnauthorizedAccessException),HttpStatusCode.Unauthorized);
}
If some exception throws and you did not assign its HttpStatusCode, then EasyResult marked it as an unhandled exception and return 500 HttpStatusCode(Or whatever that configured in ResultOption).