Search code examples
apiexceptionasp.net-corehttp-status-codesaspnetboilerplate

ASP.NET Boilerplate available list of exceptions and returned HTTP Status Codes


I am building an API with ASP.NET Boilerplate and they abstract out the error handling and returning of the HTTP Status Codes. I have looked through the documentation and it only mentions UserFriendlyException and AbpValidationException.

What are the other available exceptions I can throw with ASP.NET Boilerplate and its corresponding HTTP response codes that it returns?


Solution

  • Below are the exceptions and the correspondings status code according to the GetStatusCode() method:

    • Abp.AbpException - 500
    • Abp.AbpInitializationException - 500
    • Abp.Authorization.AbpAuthorizationException - could be 403 or 401
    • Abp.BackgroundJobs.BackgroundJobException - 500
    • Abp.Domain.Entities.EntityNotFoundException - 404
    • Abp.Domain.Uow.AbpDbConcurrencyException - 500
    • Abp.Runtime.Validation.AbpValidationException - 400
    • Abp.UI.UserFriendlyException - 500
    • Abp.WebApi.Client.AbpRemoteCallException - 500
    protected virtual HttpStatusCode GetStatusCode(HttpActionExecutedContext context)
        {
            if (context.Exception is Abp.Authorization.AbpAuthorizationException)
            {
                return AbpSession.UserId.HasValue
                    ? HttpStatusCode.Forbidden
                    : HttpStatusCode.Unauthorized;
            }
    
            if (context.Exception is AbpValidationException)
            {
                return HttpStatusCode.BadRequest;
            }
    
            if (context.Exception is EntityNotFoundException)
            {
                return HttpStatusCode.NotFound;
            }
    
            return HttpStatusCode.InternalServerError;
        }
    

    Available exceptions from docs