GlobalExceptionHandler 捕获抛出的异常,返回特定值给前端
/**
* @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 捕获抛出的异常,返回特定值给前端的更多相关文章
- 捕获Java线程池执行任务抛出的异常
捕获Java线程池执行任务抛出的异常Java中线程执行的任务接口java.lang.Runnable 要求不抛出Checked异常, public interface Runnable { publi ...
- java 检查抛出的异常是否是要捕获的检查性异常或运行时异常或错误
/** * Return whether the given throwable is a checked exception: * that is, neither a RuntimeExcepti ...
- Java中主线程如何捕获子线程抛出的异常
首先明确线程代码的边界.其实很简单,Runnable接口的run方法所界定的边界就可以看作是线程代码的边界.Runnable接口中run方法原型如下: public void run(); 而所有的具 ...
- Python面向对象之异常捕获(一)-----抛出一个异常
大部分的异常都继承自Exception这个类(而这个类有继承自BaseException这个类) 常见的异常 ValueError TypeError IndexError 抛出一个异常 下面这个类的 ...
- Spring boot 前后台分离项目 怎么处理spring security 抛出的异常
最近在开发一个项目 前后台分离的 使用 spring boot + spring security + jwt 实现用户登录权限控制等操作.但是 在用户登录的时候,怎么处理spring securi ...
- JavaWeb项目中获取对Oracle操作时抛出的异常错误码
最近在项目中碰到了这么一个需求,一个JavaWeb项目,数据库用的是Oracle.业务上有一个对一张表的操作功能,当时设置了两个字段联合的唯一约束.由于前断没有对重复字段的校验,需要在插入时如果碰到唯 ...
- 记录一次dubbo不能正常抛出特定异常
BUG场景 今天同事的代码中出现一个问题,让我帮忙排查一下.原代码大致如下 dubbo服务消费者: @Resource private IPayWayService payWayService; @R ...
- 将Controller抛出的异常转到特定View
<!-- 将Controller抛出的异常转到特定View --> <bean class="org.springframework.web.servlet.handler ...
- 使用visual studio 2015调用阿里云oss .net sdk 2.2的putobject接口抛出outofmemory异常
问题描述: 使用阿里云oss .net sdk 2.2版本,使用putobject接口上传文件时,抛出outofmemory异常. 原因分析: 上传时,用于准备上传的数据缓冲区内存分配失败.与应用软件 ...
随机推荐
- 列举 contentType: 内容类型(MIME 类型)
常用的: 1.".doc"="application/msword" 2.".pdf"="application/pdf" ...
- string参考
#include <iostream> #include <string.h> class string { private: char *data; public: stri ...
- 线程池——Executors
一 Executor框架 为了更好地控制多线程,JDK提供了一套线程框架Executor,帮助开发人员有效的进行线程控制.它们都在java.util.concurrent包中,是JDK并发包的核心.其 ...
- Kafka速览
一.基本结构 三台机器组成的Kafka集群,每台机器启动一个Kafka进程,即Broker 向broker发送消息的客户端是Producer,拉取消息的客户端是Consumer Producer和Co ...
- git——commit之后一直出现一个>
在网上搜了半天,也没见过有类似的情况,忘记具体是怎么解决的了,我记得是重新add了一遍,再commit就OK了 更新: 感谢@月下初拥的评论,找到了原因,可能是由于commit的注释结构有误造成的,比 ...
- Yii2 自定义组件
basic\components\HelloWidget namespace app\components; use yii\base\Widget; use yii\helpers\Html; cl ...
- Yii2查询语句
Yii2常用的查询: Yii2原生查询: $request = Yii::$app->request; $shopid = $request->post('shopid'); $gps = ...
- host文件是作用
什么是HOST文件:Hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Host ...
- python url编码与解码
上代码 #codeing:utf-8 from urllib import parse ori_url_10='http://192.168.0.10:3080/asg/portal.do?call= ...
- SVN工作副本已经锁定错误的解决方法
SVN工作副本锁定错误的解决方法 我们在使用svn版本控制软件时,时常会遇到想要更新本地项目的版本,却突然提示:工作副本已锁定.截图如下: 这种错误让人感觉很不舒服,实际上自己也没做过什么操作就这样了 ...