爱上MVC~业务层刻意抛出异常,全局异常的捕获它并按格式返回
对于业务层的程序的致命错误,我们一直的做法就是直接抛出指定的异常,让程序去终断,这种做法是对的,因为如果一个业务出现了致命的阻塞的问题,就没有必要再向上一层一层的返回了,但这时有个问题,直接抛异常,意味着服务器直接500了,前端如何去显示,或者如果你是API的服务,如果为前端返回,如果是500,那直接就挂了,哈哈!
下面是在MVC环境下优化的全局异常捕获代码(非API)
/// <summary>
/// 全局异常捕获
/// </summary>
public class GlobalExceptionFilterAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
JsonResult response = new JsonResult()
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
if (context.Exception is ArgumentException)
{
var exception = (ArgumentException)context.Exception;
response.Data = new
{
statusCode = System.Net.HttpStatusCode.InternalServerError,
errorcode = "",
message = exception.Message,
};
}
else if (context.Exception is Exception)
{
var exception = (Exception)context.Exception;
response.Data = new
{
statusCode = System.Net.HttpStatusCode.InternalServerError,
errorcode = "",
message = exception.Message,
};
}
else
{ response.Data = new
{
statusCode = System.Net.HttpStatusCode.InternalServerError,
errorcode = "",
message = "其它异常",
};
} context.Result = response;
context.ExceptionHandled = true;
context.HttpContext.Response.Clear();
context.HttpContext.Response.TrySkipIisCustomErrors = true;
} }
如果业务层有问题,直接就throw了
if (id == )
throw new ArgumentException("id不能为0");
if (id < )
throw new ArgumentException("id不能是负数");
然后页面后,故意让它抛出异常,我们看一下页面响应的结果

事实上,对于服务器来说,它是200,正常返回的,而对不业务模块来说,它的状态是个500,呵呵,这点要清楚.
感谢各位阅读!
爱上MVC~业务层刻意抛出异常,全局异常的捕获它并按格式返回的更多相关文章
- SpringBoot 全局异常拦截捕获处理
一.全局异常处理 //Result定义全局数据返回对象 package com.xiaobing.demo001.domain; public class Result { private Integ ...
- python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中
使用sys.excepthook函数进行全局异常的获取. 1. 使用MessageDialog实现异常显示: 2. 使用logger把捕获的异常信息输出到日志中: 步骤:定义异常处理函数, 并使用该函 ...
- SpringMvc 全局异常处理器定义,友好的返回后端错误信息
import com.google.common.collect.Maps; import org.apache.log4j.Logger; import org.springframework.be ...
- Spring MVC中@ControllerAdvice注解实现全局异常拦截
在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思. 首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经 ...
- 【spring源码学习】spring配置的事务方式是REQUIRED,但业务层抛出TransactionRequiredException异常问题
(1)spring抛出异常的点:org.springframework.orm.jpa.EntityManagerFactoryUtils public static DataAccessExcept ...
- SpringBoot全局异常的捕获设置
1.新建立一个捕获异常的实体类 如:LeeExceptionHandler package com.leecx.exception; import javax.servlet.http.HttpSer ...
- C# WinForm捕获全局异常(捕获未处理的异常)
static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...
- MVC 全局异常过滤器HandleErrorAttribute
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- springBoot 全局异常捕捉
package cn.com.cs.core.exception; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import or ...
随机推荐
- POJ(2186)强连通分量分解
#include<cstdio> #include<vector> #include<cstring> using namespace std; ; vector& ...
- Nginx正则表达式之匹配操作符详解
nginx可以在配置文件中对某些内置变量进行判断,从而实现某些功能.例如:防止rewrite.盗链.对静态资源设置缓存以及浏览器限制等等.由于nginx配置中有if指令,但是没有对应else指令,所以 ...
- web攻击之七:常见CDN回源问题总结
1. URL链接出现非法链接 (如 */./Play/show/id/349281 ); 2. CDN接收未知Referer处理失效(目前搜索引擎的状态码为499); 3. CDN抓取服务器 Cach ...
- hdu 5616 Jam's balance 正反背包+转换
http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种2.(转换的思想)在天平的两端都挑出这 ...
- openstack常见问题汇总
汇总下常见的问题以及解释下一些比较容易让人萌的参数配置等等 问题汇总1.使用纯文本模式进行复制粘贴,打死不要用word!!!可以解决绝大多数问题,如果你依然执迷不悟,那么就好自为之吧 2.创建路由器时 ...
- Go语言是如何处理栈的
转自:http://tonybai.com/2014/11/05/how-stacks-are-handled-in-go/ Go 1.4Beta1刚刚发布,在Go 1.4Beta1中,Go语言的st ...
- 前端之css样式(选择器)
一.css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,对html标签的渲染和布局 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 例如 二.c ...
- project online get approvals task data 获取审批待办任务接口
调用接口地址:重要 http://xxxx/sites/pwa/_vti_bin/PSI/ProjectServer.svc Header 三个必要参数: 其中SOAPAction写死就行,如果是on ...
- LAMP 1.2 Apache编译安装问题解决
这个错误安装 yum install -y gcc error: mod_deflate has been requested but can not be built due to prerequi ...
- restful用法
http://www.cnblogs.com/wen-wen/p/6149847.html一.创建services文件夹services文件夹下1.BaseService.jsclass Servic ...