-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalExceptionFilter.java
103 lines (88 loc) · 4.56 KB
/
GlobalExceptionFilter.java
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
package com.yangxiaochen.exception.dubbo;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.*;
import com.google.gson.Gson;
import com.yangxiaochen.exception.core.HasCode;
import com.yangxiaochen.exception.core.HasLevel;
import com.yangxiaochen.exception.core.HasTip;
import com.yangxiaochen.exception.core.level.ExceptionLevels;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Set;
@Activate(group = Constants.PROVIDER)
public class GlobalExceptionFilter implements Filter {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionFilter.class);
public static final String PARAMETER_INVALID = "PARAMETER_INVALID";
public static final String SYSTEM_ERROR = "SYSTEM_ERROR";
Gson gson = new Gson();
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws ApiException {
Result result = invoker.invoke(invocation);
if (result instanceof RpcResult && result.hasException()) {
Throwable exception = result.getException();
Throwable transformedException;
if (exception instanceof ValidationException) {
if (exception instanceof ConstraintViolationException) {
Set<ConstraintViolation<?>> constraintViolations = ((ConstraintViolationException) exception).getConstraintViolations();
StringBuilder sb = new StringBuilder();
for (ConstraintViolation constraintViolation : constraintViolations) {
sb.append(constraintViolation.getMessage()).append("|");
}
String message = sb.toString();
logWarn(invoker, invocation, exception);
transformedException = new ApiException(exception.getMessage()).code(PARAMETER_INVALID).tip("参数不合法: " + message);
transformedException.setStackTrace(exception.getStackTrace());
} else {
// 不应走到这个分支
logError(invoker, invocation, exception);
transformedException = new ApiException(exception.getMessage())
.code(PARAMETER_INVALID)
.tip("参数校验失败");
transformedException.setStackTrace(exception.getStackTrace());
}
((RpcResult) result).setException(transformedException);
return result;
}
if (exception instanceof HasLevel) {
if (((HasLevel) exception).getLevel() == ExceptionLevels.ERROR) {
logError(invoker, invocation, exception);
} else {
logWarn(invoker, invocation, exception);
}
} else {
logError(invoker, invocation, exception);
}
String code;
if (exception instanceof HasCode) {
code = ((HasCode) exception).getCode();
} else {
code = SYSTEM_ERROR;
}
String tip;
if (exception instanceof HasTip) {
tip = ((HasTip) exception).getTip();
} else {
tip = "服务器内部错误";
}
transformedException = new ApiException(exception.getMessage()).code(code).tip(tip);
transformedException.setStackTrace(exception.getStackTrace());
((RpcResult) result).setException(transformedException);
return result;
}
return result;
}
private void logWarn(Invoker<?> invoker, Invocation invocation, Throwable exception) {
logger.warn(String.format("dubbo invoke exception: %s, from: %s, interface: %s, method: %s, params: %s",
exception.getMessage(), RpcContext.getContext().getRemoteHost(), invoker.getInterface().getName(), invocation.getMethodName(), gson.toJson(invocation.getArguments())
), exception);
}
private void logError(Invoker<?> invoker, Invocation invocation, Throwable exception) {
logger.error(String.format("dubbo invoke exception: %s, from: %s, interface: %s, method: %s, params: %s",
exception.getMessage(), RpcContext.getContext().getRemoteHost(), invoker.getInterface().getName(), invocation.getMethodName(), gson.toJson(invocation.getArguments())
), exception);
}
}