如何友好的处理 WebApi 中抛出的错误

开发 架构
微软的 ASP.NET Web API 是一个轻量级的web框架,可用来构建基于 http 无状态的rest服务,异常是一种运行时错误,异常处理是一种处理运行时错误的技术,每一个开发者都应该知道如何处理 Web API 中的异常,并且在 Action 中使用合适的 错误码 和 错误信息 进行包装。

[[384418]]

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。

微软的 ASP.NET Web API 是一个轻量级的web框架,可用来构建基于 http 无状态的rest服务,异常是一种运行时错误,异常处理是一种处理运行时错误的技术,每一个开发者都应该知道如何处理 Web API 中的异常,并且在 Action 中使用合适的 错误码 和 错误信息 进行包装。

WebAPI 中的 HttpResponseException

你可以在 Action 中使用 HttpResponseException 来包装指定的 HttpCode 和 HttpMessage,如下例子所示:

  1. public Employee GetEmployee(int id) 
  2.     Employee emp = employeeRepository.Get(id); 
  3.     if (emp == null
  4.     { 
  5.         var response = new HttpResponseMessage(HttpStatusCode.NotFound) 
  6.         { 
  7.             Content = new StringContent("Employee doesn't exist", System.Text.Encoding.UTF8, "text/plain"), 
  8.             StatusCode = HttpStatusCode.NotFound 
  9.         } 
  10.         throw new HttpResponseException(response); 
  11.     } 
  12.     return emp; 

如果你的 Action 返回的是 IHttpActionResult,那么可将 GetEmployee() 方法修改如下:

  1. public IHttpActionResult GetEmployee(int id) 
  2.     Employee emp = employeeRepository.Get(id); 
  3.     if (emp == null
  4.     { 
  5.         var response = new HttpResponseMessage(HttpStatusCode.NotFound) 
  6.         { 
  7.             Content = new StringContent("Employee doesn't exist", System.Text.Encoding.UTF8, "text/plain"), 
  8.             StatusCode = HttpStatusCode.NotFound 
  9.         } 
  10.         throw new HttpResponseException(response); 
  11.     } 
  12.     return Ok(emp); 

从上面的代码可以看出,错误码 和 错误消息 都赋给了 Response 对象,然后包装到了 HttpResponseException 进行返回。

WebAPI 中使用 HttpError

除了直接实例化 HttpResponseMessage 类,还可以使用 Request.CreateErrorResponse() 快捷的创建 HttpResponseMessage 类,如下代码所示:

  1. public IActionResult GetEmployee(int id) 
  2.     Employee emp = employeeRepository.Get(id); 
  3.     if (emp == null
  4.     { 
  5.        string message = "Employee doesn't exist"
  6.         throw new HttpResponseException( 
  7.             Request.CreateErrorResponse(HttpStatusCode.NotFound, message)); 
  8.     } 
  9.     return Ok(emp); 

WebAPI 中使用 异常过滤器

异常过滤器是一种可以在 WebAPI 中捕获那些未得到处理的异常的过滤器,要想创建异常过滤器,你需要实现 IExceptionFilter 接口,不过这种方式比较麻烦,更快捷的方法是直接继承 ExceptionFilterAttribute 并重写里面的 OnException() 方法即可,这是因为 ExceptionFilterAttribute 类本身就实现了 IExceptionFilter 接口,如下代码所示:

  1. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] 
  2.    public abstract class ExceptionFilterAttribute : FilterAttribute, IExceptionFilter, IFilter 
  3.    { 
  4.  
  5.        protected ExceptionFilterAttribute(); 
  6.  
  7.        public virtual void OnException(HttpActionExecutedContext actionExecutedContext); 
  8.        public virtual Task OnExceptionAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken); 
  9.    } 

下面的代码片段展示了如何通过重写 ExceptionFilterAttribute.OnException() 方法来创建一个自定义异常过滤器,请注意下面的代码是如何捕获在 Action 中抛出的异常,并将捕获到的异常转换为 HttpStatusResponse 实体,然后塞入合适的 httpcode 和 httpmessage,如下代码所示:

  1. public class CustomExceptionFilter : ExceptionFilterAttribute 
  2.  { 
  3.      public override void OnException(HttpActionExecutedContext actionExecutedContext) 
  4.      { 
  5.          HttpStatusCode status = HttpStatusCode.InternalServerError; 
  6.          String message = String.Empty; 
  7.          var exceptionType = actionExecutedContext.Exception.GetType(); 
  8.          if (exceptionType == typeof(UnauthorizedAccessException)) 
  9.          { 
  10.              message = "Access to the Web API is not authorized."
  11.              status = HttpStatusCode.Unauthorized; 
  12.          } 
  13.          else if (exceptionType == typeof(DivideByZeroException)) 
  14.          { 
  15.              message = "Internal Server Error."
  16.              status = HttpStatusCode.InternalServerError; 
  17.          } 
  18.          else 
  19.          { 
  20.              message = "Not found."
  21.              status = HttpStatusCode.NotFound; 
  22.          } 
  23.          actionExecutedContext.Response = new HttpResponseMessage() 
  24.          { 
  25.              Content = new StringContent(message, System.Text.Encoding.UTF8, "text/plain"), 
  26.              StatusCode = status 
  27.          }; 
  28.          base.OnException(actionExecutedContext); 
  29.      } 
  30.  } 

接下来将自定义的异常过滤器添加到 HttpConfiguration 全局集合中,如下代码所示:

  1. public static void Register(HttpConfiguration config) 
  2.         { 
  3.             config.MapHttpAttributeRoutes(); 
  4.             config.Routes.MapHttpRoute( 
  5.                 name"DefaultApi"
  6.                 routeTemplate: "api/{controller}/{id}"
  7.                 defaults: new { id = RouteParameter.Optional } 
  8.             ); 
  9.             config.Formatters.Remove(config.Formatters.XmlFormatter); 
  10.             config.Filters.Add(new CustomExceptionFilter()); 
  11.         } 

除了将自定义异常设置到全局上,你还可以缩小粒度到 Controller 或者 Action 级别上,下面的代码分别展示了如何将其控制在 Action 和 Controller 上。

  1. [DatabaseExceptionFilter] 
  2. public class EmployeesController : ApiController 
  3.     //Some code 
  4.  
  5.  [CustomExceptionFilter] 
  6.  public IEnumerable<string> Get() 
  7.  { 
  8.     throw new DivideByZeroException();  
  9.  } 

ASP.NET Web API 提供了强大的 HttpResponseException 来包装异常信息,默认情况下,当 WebAPI 中抛出异常,系统默认使用 Http StateCode = 500 作为回应,也即:Internal Server Error. ,场景就来了,如果你会用 HttpResponseException 的话,就可以改变这种系统默认行为,自定义错误码和错误信息让结果更加清晰语义化。

译文链接:https://www.infoworld.com/article/2994111/how-to-handle-errors-in-aspnet-web-api.html

 

责任编辑:武晓燕 来源: 码农读书
相关推荐

2009-03-18 08:59:28

throw异常Java

2021-01-14 21:37:01

JavaScript开发代码

2014-06-27 09:34:03

AngularJS

2023-11-30 07:15:36

GolangRecover

2023-12-26 22:05:53

并发代码goroutines

2021-09-01 07:21:46

堆栈Gopanic

2016-09-07 20:28:17

MySQL存储数据库

2024-04-16 12:18:05

编程异常处理错误返回

2018-03-05 19:20:49

LinuxWordPressHTTP

2017-05-26 11:32:44

程序应用测试

2021-01-28 13:16:27

Python编程语言

2023-10-26 12:05:14

Golang开发

2023-04-17 07:41:02

Rust网络数据

2018-08-30 10:28:05

修复Windows 10IntcOED

2021-04-14 07:08:14

Nodejs错误处理

2022-02-15 08:38:04

错误逻辑异常编程程序

2024-03-05 18:15:28

AsyncAwait前端

2021-03-02 09:12:25

Java异常机制

2022-05-06 08:00:51

Golang编程语言Java

2023-10-26 15:49:53

Go日志
点赞
收藏

51CTO技术栈公众号