Home > Software design >  Core 7 - Api error handling, model state validation UseExceptionhandler
Core 7 - Api error handling, model state validation UseExceptionhandler

Time:02-01

I am currently working on implementing some Apis using swagger/swashbuckle in net core 7 and implementing some error handling, I've gone down the route of using an exception handler. With separate endpoints from dev/prod. E.g. Startup.cs

            if (env.IsDevelopment())
            {
                ...details ommited
                app.UseExceptionHandler("/dev-error");
            }
            else
            {
                ...details ommited
                app.UseExceptionHandler("/error");
            }

ErrorController.cs

[AllowAnonymous]
[ApiExplorerSettings(IgnoreApi = true)]
public class ErrorController : Controller
{
    private ILogger _logger;

    public ErrorController(ILogger logger)
    {
        _logger = logger;
    }

    [Route("dev-error")]
    public IAttempt DevError()
    {
        var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
        var exception = context.Error;

        return Attempt.Fail(exception);
    }

    [Route("error")]
    public IAttempt Error()
    {
        var context = HttpContext.Features.Get<IExceptionHandlerFeature>();
        var exception = context.Error;

        _logger.Log(LogLevel.Error, exception, exception.Message);

        switch (exception)
        {
            case UnauthorizedAccessException:
                Response.StatusCode = (int) HttpStatusCode.Unauthorized;
                return Attempt.Fail("Unauthorised");

            default:
                Response.StatusCode = (int) HttpStatusCode.InternalServerError;
                return Attempt.Fail("Generic Error");
        }
    }
}

The idea is that all responses are of IAttempt, so that the FE user can check if its succeeded etc. and whether to handle the result or exception in a user friendly way.

This has been working great up until now when I've been implementing Api's that require the model to be validated. I wanted to amend the IAttempt class to provide modelstate feedback, however I have tried many approaches and cant seem to get modelstate validation flow through the exception handler.

I wanted to implement a custom ValidationException that contains the errors which is then handled in these controllers. But when an exception is thrown in either an IActionFilter or when overriding the InvalidModelStateResponseFactory the exception isn't caught by the exception handler.

Is there a work around? Am I missing something?

Alternatively I could define a InvalidModelStateResponseFactory that returns a similar model(IAttempt), but it would be nice for Failed requests to be handled in one place.

Cheers in advance

CodePudding user response:

I think you can make the InvalidModelStateResponseFactory redirect to the ErrorController, sending the required data to create your response

CodePudding user response:

According to your description, I suggest you could consider using the customer action filter to achieve your requirement.

Inside the custom action filter, we could get the model state's results, then you could throw the exception inside it.

More details, you could refer to below codes:

1.Create the custom action filter:

public class CustomValidationActionFilter : IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext context)
    {
        if (!context.ModelState.IsValid)
        {
            var errorList = context.ModelState.Values
                .SelectMany(m => m.Errors)
                .Select(m => m.ErrorMessage)
                .ToList();

            throw new Exception();
        }


     }

    public void OnActionExecuting(ActionExecutingContext context) { }
}

2.Inside the program.cs

builder.Services.AddControllersWithViews(options =>
{
    options.Filters.Add(new CustomValidationActionFilter());
});

Then if it thrown the exception, it will go to the controller's error action method, since you set the global exception handler.

  • Related