Quantcast
Channel: ASP.NET Core Web API exception handling - Stack Overflow
Viewing all articles
Browse latest Browse all 13

Answer by r.pedrosa for ASP.NET Core Web API exception handling

$
0
0

By adding your own "Exception Handling Middleware", makes it hard to reuse some good built-in logic of Exception Handler like send an "RFC 7807-compliant payload to the client" when an error happens.

What I made was to extend built-in Exception handler outside of the Startup.cs class to handle custom exceptions or override the behavior of existing ones. For example, an ArgumentException and convert into BadRequest without changing the default behavior of other exceptions:

on the Startup.cs add:

app.UseExceptionHandler("/error");

and extend ErrorController.cs with something like this:

using System;using Microsoft.AspNetCore.Diagnostics;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Hosting;namespace Api.Controllers{    [ApiController]    [ApiExplorerSettings(IgnoreApi = true)]    [AllowAnonymous]    public class ErrorController : ControllerBase    {        [Route("/error")]        public IActionResult Error(            [FromServices] IWebHostEnvironment webHostEnvironment)        {            var context = HttpContext.Features.Get<IExceptionHandlerFeature>();            var exceptionType = context.Error.GetType();            if (exceptionType == typeof(ArgumentException)                || exceptionType == typeof(ArgumentNullException)                || exceptionType == typeof(ArgumentOutOfRangeException))            {                if (webHostEnvironment.IsDevelopment())                {                    return ValidationProblem(                        context.Error.StackTrace,                        title: context.Error.Message);                }                return ValidationProblem(context.Error.Message);            }            if (exceptionType == typeof(NotFoundException))            {                return NotFound(context.Error.Message);            }            if (webHostEnvironment.IsDevelopment())            {                return Problem(                    context.Error.StackTrace,                    title: context.Error.Message                    );            }            return Problem();        }    }}

Note that:

  1. NotFoundException is a custom exception and all you need to do is throw new NotFoundException(null); or throw new ArgumentException("Invalid argument.");
  2. You should not serve sensitive error information to clients. Serving errors is a security risk.

Viewing all articles
Browse latest Browse all 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>