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

不羁岁月 提交于 2020-01-07 04:45:27

问题


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?


回答1:


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




回答2:


The list of available exceptions are;

  • AbpException
  • UserFriendlyException
  • AbpRemoteCallException
  • AbpValidationException
  • BackgroundJobException
  • EntityNotFoundException
  • AbpAuthorizationException
  • AbpDbConcurrencyException
  • AbpInitializationException


来源:https://stackoverflow.com/questions/45029407/asp-net-boilerplate-available-list-of-exceptions-and-returned-http-status-codes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!