问题
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