DefaultExceptionHandler.java
4.38 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.huaheng.framework.web.exception;
import com.alibaba.fastjson.JSONException;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.framework.web.domain.RetCode;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.huaheng.common.exception.DemoModeException;
import com.huaheng.common.utils.security.PermissionUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义异常处理器
*
* @author huaheng
*/
@RestControllerAdvice
public class DefaultExceptionHandler
{
private static final Logger log = LoggerFactory.getLogger(DefaultExceptionHandler.class);
/**
* 权限校验失败
*/
@ExceptionHandler(AuthorizationException.class)
public AjaxResult handleAuthorizationException(AuthorizationException ex)
{
log.error("权限异常:" + ex.getMessage(), ex);
return AjaxResult.setResult(RetCode.UNAUTHEN, PermissionUtils.getMsg(ex.getMessage()), null);
}
/**
* JSON异常统一处理
*/
@ExceptionHandler(value = JSONException.class)
public AjaxResult jsonExceptionHandler(JSONException ex) {
log.error("转化JSON异常:" + ex.getMessage(), ex);
return AjaxResult.error("转化JSON异常:" + ex.getMessage());
}
/**
* 方法参数验证异常的处理
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public AjaxResult validatedExceptionHandler(MethodArgumentNotValidException ex) {
StringBuilder sb = new StringBuilder();
for (ObjectError errorItem : ex.getBindingResult().getAllErrors()) {
sb.append(" " + errorItem.getDefaultMessage() + ",");
}
sb.deleteCharAt(sb.length() - 1);
log.error("方法参数验证出现异常:" + sb.toString(), ex);
return AjaxResult.error("方法参数验证出现异常:" + sb.toString());
}
/**
* 业务异常的处理
*/
@ExceptionHandler(value = ServiceException.class)
public AjaxResult serviceExceptionHandler(ServiceException ex) {
log.error("业务处理出现异常:" + ex.getMessage(), ex);
return AjaxResult.error("业务处理出现异常:" + ex.getMessage());
}
/**
* 请求方式不支持
*/
@ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
public AjaxResult handleException(HttpRequestMethodNotSupportedException ex)
{
log.error("不支持' " + ex.getMethod() + "'请求:" + ex.getMessage(), ex);
return AjaxResult.error("网络请求异常:不支持' " + ex.getMethod() + "'请求");
}
/**
* 拦截未知的运行时异常
*/
@ExceptionHandler(RuntimeException.class)
public AjaxResult notFount(RuntimeException ex)
{
StringBuilder sb = new StringBuilder();
StackTraceElement[] stacks = ex.getStackTrace();
int length = stacks.length > 3 ? 3 : stacks.length;
for (int i=0; i< length; i++) {
sb.append("文件:" + stacks[i].getFileName());
sb.append("类名:" + stacks[i].getClassName());
sb.append("方法:" + stacks[i].getMethodName());
sb.append("行数:" + stacks[i].getLineNumber());
sb.append(System.getProperty("line.separator"));
}
log.error("运行时异常:" + ex.getMessage(), ex);
log.error(sb.toString());
return AjaxResult.error("运行时异常:" + ex.getMessage());
}
/**
* 系统异常
*/
@ExceptionHandler(Exception.class)
public AjaxResult handleException(Exception ex)
{
log.error("服务器错误:" + ex.getMessage(), ex);
return AjaxResult.setResult(RetCode.INTERNAL_SERVER_ERROR ,"服务器错误:" + ex.getMessage(), ex);
}
/**
* 演示模式异常
*/
@ExceptionHandler(DemoModeException.class)
public AjaxResult demoModeException(DemoModeException e)
{
return AjaxResult.error("演示模式,不允许操作");
}
}