GlobalExceptionHandler.java
3.95 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
package com.glsx.admin.exception;
import java.util.List;
import java.util.concurrent.TimeoutException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-16
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler({BusinessException.class})
public R handleBusinessException(BusinessException e) {
log.error("!!!! 业务异常", e);
return R.error(e.getCode(), e.getMessage());
}
@ExceptionHandler({ServletRequestBindingException.class, MethodArgumentNotValidException.class,
BindException.class})
public R handleBindException(Exception e) {
log.error("!!!! 参数错误", e);
if ((e instanceof MethodArgumentNotValidException) || (e instanceof BindException)) {
List<FieldError> errorList;
if (e instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
errorList = ex.getBindingResult().getFieldErrors();
} else {
BindException ex = (BindException) e;
errorList = ex.getFieldErrors();
}
StringBuffer messageSb = new StringBuffer();
for (FieldError error : errorList) {
String fieldMessage = String.format("%s: %s, ",
error.getField(), error.getDefaultMessage());
messageSb.append(fieldMessage);
}
return R.error(ResultEnum.PARAM_ERROR.getCode(),
messageSb.toString().replaceAll(", +$", ""));
} else {
return R.error(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
}
}
@ExceptionHandler({HystrixRuntimeException.class})
public R handleHystrixException(HystrixRuntimeException e) {
Throwable cause = e.getCause();
if (cause instanceof FeignException) {
FeignException feignException = (FeignException) cause;
return this.handleFeignException(feignException);
} else if (cause instanceof TimeoutException) {
log.error("!!!! 服务调用超时", e);
return R.error(ResultEnum.REMOTE_SERVER_TIME_OUT.getCode(),
ResultEnum.REMOTE_SERVER_TIME_OUT.getMessage());
} else {
log.error("!!!! Hystrix熔断错误", e);
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
}
}
@ExceptionHandler({FeignException.class})
public R handleFeignException(FeignException e) {
log.error("!!!! 远程服务错误", e);
String respMessage;
if (StringUtils.isNotBlank(e.contentUTF8())) {
JSONObject respObj = JSON.parseObject(e.contentUTF8());
respMessage = respObj.getString("message");
} else {
respMessage = "";
}
int code = ResultEnum.REMOTE_SERVICE_ERROR.getCode();
String message = StringUtils.isBlank(respMessage)
? ResultEnum.REMOTE_SERVICE_ERROR.getMessage() : respMessage;
return R.error(code, message);
}
@ExceptionHandler(Exception.class)
public R handleException(Exception e) {
log.error("!!!! 未知异常", e);
return R.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
}
}