# WebApi 异常处理解决方案

作者:网络 时间: 2020-10-16

# 一、使用异常筛选器捕获所有异常

我们知道,一般情况下,WebApi 作为服务使用,每次客户端发送 http 请求到我们的 WebApi 服务里面,服务端得到结果输出 response 到客户端。这个过程中,一旦服务端发生异常,会统一向客户端返回 500 的错误。

[HttpGet]
public string GetAllChargingData([FromUri]TB_CHARGING obj)
{
    throw new NotImplementedException("方法不被支持");
}
1
2
3
4
5

我们来看看 http 请求

img

而有些时候,我们客户端需要得到更加精确的错误码来判断异常类型,怎么办呢?

记得在介绍 AOP 的时候,我们介绍过 MVC 里面的 IExceptionFilter 接口,这个接口用于定义异常筛选器所需的方法,在 WebApi 里面,也有这么一个异常筛选器,下面我们通过一个实例来看看具体如何实现。

首先在 App_Start 里面新建一个类 WebApiExceptionFilterAttribute.cs,继承 ExceptionFilterAttribute,重写 OnException 方法

    public class WebApiExceptionFilterAttribute : ExceptionFilterAttribute
    {
        //重写基类的异常处理方法
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            //1.异常日志记录(正式项目里面一般是用log4net记录异常日志)
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "——" +
                actionExecutedContext.Exception.GetType().ToString() + ":" + actionExecutedContext.Exception.Message + "——堆栈信息:" +
                actionExecutedContext.Exception.StackTrace);

            //2.返回调用方具体的异常信息
            if (actionExecutedContext.Exception is NotImplementedException)
            {
                actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
            }
            else if (actionExecutedContext.Exception is TimeoutException)
            {
                actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.RequestTimeout);
            }
            //.....这里可以根据项目需要返回到客户端特定的状态码。如果找不到相应的异常,统一返回服务端错误500
            else
            {
                actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
            }

            base.OnException(actionExecutedContext);
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

代码解析:**通过判断异常的具体类型,向客户端返回不同的 http 状态码,示例里面写了两个,可以根据项目的实际情况加一些特定的我们想要捕获的异常,然后将对应的状态码写入 http 请求的 response 里面,对于一些我们无法判断类型的异常,统一返回服务端错误 500。**关于 http 的状态码,framework 里面定义了一些常见的类型,我们大概看看:

img Http 状态码

定义好了异常处理方法,剩下的就是如何使用了。可以根据实际情况,在不同级别使用统一的异常处理机制。

# 1、接口级别

        [WebApiExceptionFilter]
        [HttpGet]
        public string GetAllChargingData([FromUri]TB_CHARGING obj)
        {
            throw new NotImplementedException("方法不被支持");
        }
1
2
3
4
5
6

执行到异常后,会先进到 OnException 方法:

img

执行完成之后浏览器查看:

img

如果需要,甚至可以向 Status Code 里面写入自定义的描述信息,并且还可以向我们的 Response 的 Content 里面写入我们想要的信息。我们稍微改下 OnException 方法:

       if (actionExecutedContext.Exception is NotImplementedException)
            {
                var oResponse = new HttpResponseMessage(HttpStatusCode.NotImplemented);
                oResponse.Content = new StringContent("方法不被支持");
                oResponse.ReasonPhrase = "This Func is Not Supported";
                actionExecutedContext.Response = oResponse;
            }
1
2
3
4
5
6
7

看看 ReasonPhrase 描述信息

img

看看 Response 的描述信息

img

# 2、控制器级别

如果想要某一个或者多个控制器里面的所有接口都使用异常过滤,直接在控制器上面标注特性即可。

  • 某一个控制器上面启用异常过滤
  [WebApiExceptionFilter]
    public class ChargingController : BaseApiController
    {
        #region Get
        [HttpGet]
        public string GetAllChargingData([FromUri]TB_CHARGING obj)
        {
            throw new NotImplementedException("方法不被支持");
        }
    }
1
2
3
4
5
6
7
8
9
10
  • 多个控制器上面同时启用异常过滤
    [WebApiExceptionFilter]
    public class BaseApiController : ApiController
    {
    }
1
2
3
4
    public class ChargingController : BaseApiController
    {
        #region Get
        [HttpGet]
        public string GetAllChargingData([FromUri]TB_CHARGING obj)
        {
            throw new NotImplementedException("方法不被支持");
        }
    }
1
2
3
4
5
6
7
8
9

这样,所有继承 BaseApiController 的子类都会启用异常过滤。

# 3、全局配置

如果需要对整个应用程序都启用异常过滤,则需要做如下两步:

1、在 Global.asax 全局配置里面添加 GlobalConfiguration.Configuration.Filters.Add(new WebApiExceptionFilterAttribute()); 这一句,如下:

void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            GlobalConfiguration.Configuration.Filters.Add(new WebApiExceptionFilterAttribute());
        }
1
2
3
4
5
6
7
8

2、在 WebApiConfig.cs 文件的 Register 方法里面添加 config.Filters.Add(new WebApiExceptionFilterAttribute()); 这一句,如下:

        public static void Register(HttpConfiguration config)
        {
            //跨域配置
            config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

            // Web API 路由
            config.MapHttpAttributeRoutes();

            RouteTable.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            ).RouteHandler = new SessionControllerRouteHandler();

            config.Filters.Add(new WebApiExceptionFilterAttribute());
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 二、HttpResponseException 自定义异常信息

上面说的是全局的异常捕获以及处理方式,在某些情况下,我们希望以异常的方式向客户端发送相关信息,可能就需要用到我们的 HttpResponseException。比如:

        [HttpGet]
        public TB_CHARGING GetById(string id)
        {
            //从后台查询实体
            var oModel = server.Find(id);
            if (oModel == null)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(string.Format("没有找到id={0}的对象", id)),
                    ReasonPhrase = "object is not found"
                };
                throw new HttpResponseException(resp);
            }
            return oModel;
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

执行之后浏览器里面查看结果:

img

img

代码释疑:细心的朋友可能,发现了,这里既使用了 HttpResponseMessage,又使用了 HttpResponseException,那么,像这种可控的异常,我们是否可以直接以 HttpResponseMessage 的形式直接返回到客户端而不用抛出异常呢?这里就要谈谈这两个对象的区别了,博主的理解是 HttpResonseMessage 对象用来响应讯息并包含状态码及数据内容,**HttpResponseException 对象用来向客户端返回包含错误讯息的异常。**

**在网上看到一篇 文章 这样描述两者的区别:当呼叫 Web API 服务时发生了与预期上不同的错误时,理当应该中止程序返回错误讯息,这时对于错误的返回就该使用 HttpResponseException,而使用 HttpResponseMessage 则是代表着当客户端发送了一个工作请求而 Web API 正确的完成了这个工作,就能够使用 HttpResponseMessage 返回一个 201 的讯息,所以 HttpResponseMessage 与 HttpResponseException 在使用上根本的目标就是不同的,用 HttpResponseMessage 去返回一个例外错误也会让程序结构难以辨别且不够清晰。**

# 三、返回 HttpError

HttpError 对象提供一致的方法来响应正文中返回错误的信息。准确来说,HttpError 并不是一个异常,只是用来包装错误信息的一个对象。其实在某一定的程度上,HttpError 和 HttpResponseMessage 使用比较相似,二者都可以向客户端返回 http 状态码和错误讯息,并且都可以包含在 HttpResponseException 对象中发回到客户端。但是,一般情况下,HttpError 只有在向客户端返回错误讯息的时候才会使用,而 HttpResponseMessage 对象既可以返回错误讯息,也可返回请求正确的消息。其实关于 HttpError 没什么特别好讲的,我们来看一个例子就能明白:

    public HttpResponseMessage Update(dynamic obj)
        {
            TB_Product oModel = null;
            try
            {
                var id = Convert.ToString(obj.id);
                oModel = Newtonsoft.Json.JsonConvert.DeserializeObject<TB_Product>(Convert.ToString(obj.dataModel));

                //...复杂的业务逻辑


            }
            catch(Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
            }

            return Request.CreateResponse<TB_Product>(HttpStatusCode.OK, oModel);

        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

假如现在在执行 try 里面复杂业务逻辑的时候发生了异常,我们捕获到了异常然后向客户端返回 HttpError 对象,这个对象里面包含我们自定义的错误讯息,如果正常则返回 HttpResponseMessage 对象。

如果请求异常:

img

如果请求正常

img

# 四、总结

以上三种异常的处理方法,可以根据不同的场景选择使用。

  • 如果项目对异常处理要求并不高,只需要记录好异常日志即可,那么使用异常筛选器就能够搞定
  • 如果项目需要对不同的异常,客户端做不同的处理。而这个时候使用异常筛选器不能详尽所有的异常,可能使用 HttpResponseException 对象是更好的选择,定义更加精细的异常和异常描述。
  • 对于何时使用 HttpError,又何时使用 HttpResponseMessage,可以参考上文三里面用法。
  • 当然实际项目中很可能以上两种或者三种同时使用。

上文通过一些简单的示例介绍了下 WebApi 里面异常的处理机制,可能不够深入,但对于一般项目的异常处理基本够用。其实有一点博主还没有想明白,对于构造函数里面的异常该如何统一捕获呢?通过异常筛选器是捕获不到的,不知道园友们有没有什么更好的办法,不吝赐教,感谢感谢!如果本文能帮到你,不妨推荐下,您的推荐是博主继续总结的动力!