/**
* @author hhh
* @date 2019/1/17 16:28
* @Despriction
*/
@ResponseBody
@ControllerAdvice
@Log4j2
public class GlobalExceptionHandler {
/**
* 通用错误一般用于 catch 抛出异常的时候 详情见消息体
*/
public static final Integer GENERAL_ERROR = 604; //处理通用异常
@ExceptionHandler(value = Exception.class)
public BaseResMessage defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
BaseResMessage<Entity> message = new BaseResMessage<>();
log.error("非捕获异常{}", e);
StackTraceElement[] stackTrace = e.getStackTrace();
StringBuilder sb = new StringBuilder("[系统错误!" + e.getClass().getName() + ": " + e.getLocalizedMessage() + "]\n");
for (StackTraceElement stackTraceElement : e.getStackTrace()) {
String s = stackTraceElement.toString();
if (s.startsWith("com.jn")) {
sb.append(s).append("\n");
}
}
return message.error(GENERAL_ERROR, sb.toString());
} @ExceptionHandler(CustomException.class)//可以将Exception中的异常信息以Json的信息返回给前端
public BaseResMessage handle(CustomException e) {
log.error("状态码:" + e.getErrorCode(), e);
return this.error(e);
} public static BaseResMessage error(CustomException e) {
BaseResMessage baseResMessage = new BaseResMessage();
baseResMessage.setStatus(e.getErrorCode());
baseResMessage.setMsg(e.getMessage());
return baseResMessage;
}
}
public class CustomException extends RuntimeException {

    private static final long serialVersionUID = 1L;
public static final Integer UNKNOWN_ERROR = -1;
/**
* 错误编码
*/
private int errorCode = UNKNOWN_ERROR; /**
* 消息是否为属性文件中的Key
*/
private boolean propertiesKey = true; /**
* 构造一个基本异常.
*
* @param message
* 信息描述
*/
public CustomException(String message)
{
super(message);
} /**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
*/
public CustomException(int errorCode, String message)
{
this(errorCode, message, true);
} /**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
*/
public CustomException(int errorCode, String message, Throwable cause)
{
this(errorCode, message, cause, true);
} /**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
* @param propertiesKey
* 消息是否为属性文件中的Key
*/
public CustomException(int errorCode, String message, boolean propertiesKey)
{
super(message);
this.setErrorCode(errorCode);
this.setPropertiesKey(propertiesKey);
} /**
* 构造一个基本异常.
*
* @param errorCode
* 错误编码
* @param message
* 信息描述
*/
public CustomException(int errorCode, String message, Throwable cause, boolean propertiesKey)
{
super(message, cause);
this.setErrorCode(errorCode);
this.setPropertiesKey(propertiesKey);
} /**
* 构造一个基本异常.
*
* @param message
* 信息描述
* @param cause
* 根异常类(可以存入任何异常)
*/
public CustomException(String message, Throwable cause)
{
super(message, cause);
} public int getErrorCode() {
return errorCode;
} public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
} public boolean isPropertiesKey()
{
return propertiesKey;
} public void setPropertiesKey(boolean propertiesKey)
{
this.propertiesKey = propertiesKey;
} }

GlobalExceptionHandler 捕获抛出的异常,返回特定值给前端的更多相关文章

  1. 捕获Java线程池执行任务抛出的异常

    捕获Java线程池执行任务抛出的异常Java中线程执行的任务接口java.lang.Runnable 要求不抛出Checked异常, public interface Runnable { publi ...

  2. java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误

    /** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...

  3. Java中主线程如何捕获子线程抛出的异常

    首先明确线程代码的边界.其实很简单,Runnable接口的run方法所界定的边界就可以看作是线程代码的边界.Runnable接口中run方法原型如下: public void run(); 而所有的具 ...

  4. Python面向对象之异常捕获(一)-----抛出一个异常

    大部分的异常都继承自Exception这个类(而这个类有继承自BaseException这个类) 常见的异常 ValueError TypeError IndexError 抛出一个异常 下面这个类的 ...

  5. Spring boot 前后台分离项目 怎么处理spring security 抛出的异常

    最近在开发一个项目 前后台分离的 使用 spring boot + spring security + jwt 实现用户登录权限控制等操作.但是 在用户登录的时候,怎么处理spring  securi ...

  6. JavaWeb项目中获取对Oracle操作时抛出的异常错误码

    最近在项目中碰到了这么一个需求,一个JavaWeb项目,数据库用的是Oracle.业务上有一个对一张表的操作功能,当时设置了两个字段联合的唯一约束.由于前断没有对重复字段的校验,需要在插入时如果碰到唯 ...

  7. 记录一次dubbo不能正常抛出特定异常

    BUG场景 今天同事的代码中出现一个问题,让我帮忙排查一下.原代码大致如下 dubbo服务消费者: @Resource private IPayWayService payWayService; @R ...

  8. 将Controller抛出的异常转到特定View

    <!-- 将Controller抛出的异常转到特定View --> <bean class="org.springframework.web.servlet.handler ...

  9. 使用visual studio 2015调用阿里云oss .net sdk 2.2的putobject接口抛出outofmemory异常

    问题描述: 使用阿里云oss .net sdk 2.2版本,使用putobject接口上传文件时,抛出outofmemory异常. 原因分析: 上传时,用于准备上传的数据缓冲区内存分配失败.与应用软件 ...

随机推荐

  1. windows 添加永久路由

    打开cmd 命令为: route  add  目的网络    mask   子网掩码     下一跳   -p 添加完成后 可以使用如下命令进行查看: route print

  2. 【LeetCode】水题(刚开始重新刷题找感觉用的)

    [9] Palindrome Number [Easy] 给一个数字,用不转化成字符串的方式判断它是否是回文. 先求数字长度,然后把数字的后半段做翻转(就是不断地取模,除10这种方式),然后判断前后半 ...

  3. elasticsearch启动问题

    ES安装完一直启动不了,问题解决. 报错: ERROR: bootstrap checks failed system call filters failed to install; check th ...

  4. liunx下在线安装mysql

    一:在线安装mysql 1.首先检测一下,mysql之前有没有被安装 命令:rpm -qa | grep mysql 2.删除mysql的命令: rpm -e --nodeps `rpm -qa | ...

  5. leetcode-168周赛-1298-你能从盒子中获得的最大糖果数

    题目描述: 方法一:bfs class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List ...

  6. leetcode-12双周赛-1244-力扣排行榜

    题目描述: class Leaderboard: def __init__(self): self.map = collections.Counter() def addScore(self, pla ...

  7. java——IO(普通文件,二进制文件,压缩文件 )

    二进制文件 压缩包

  8. JCF——工具类

  9. ac自动机fail树上按询问建立上跳指针——cf963D

    解法看着吓人,其实就是为了优化ac自动机上暴力跳fail指针.. 另外这题对于复杂度的分析很有学习价值 /* 给定一个母串s,再给定n个询问(k,m) 对于每个询问,求出长度最小的t,使t是s的子串, ...

  10. Extjs各版本的下载链接,包含ext3.4源码示例

    最近在维护一个老平台,用的是ext3.4,老东西在网上找示例发现既然还有人收钱,谷歌了一下总算找到了一位免费的发布的,感谢 yipanbo 分享 Extjs的版本繁多,本文收集了Extjs各个版本的下 ...