ExceptionAdvice.java
6.37 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.infoloop.tianting.advice;
import cn.dev33.satoken.exception.SaTokenException;
import com.infoloop.tianting.exception.ClientEndException;
import com.infoloop.tianting.exception.ClientEndExceptions;
import com.infoloop.tianting.model.common.ResponseResult;
import io.grpc.StatusRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.stream.Collectors;
@RestControllerAdvice
@Slf4j
public class ExceptionAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
public ResponseResult<String> handleValidationException(Exception e) {
String resp = null;
if (e instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
resp = ex.getBindingResult().getAllErrors().stream()
.map(ObjectError::getDefaultMessage)
.collect(Collectors.joining("; "));
} else if (e instanceof ConstraintViolationException) {
ConstraintViolationException ex = (ConstraintViolationException) e;
resp = ex.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.joining("; "));
} else if (e instanceof BindException) {
BindException ex = (BindException) e;
resp = ex.getAllErrors().stream()
.map(ObjectError::getDefaultMessage)
.collect(Collectors.joining("; "));
} else if (e instanceof ValidationException) {
ValidationException ex = (ValidationException) e;
resp = ex.getMessage();
}
return ResponseResult.validateFailed(resp);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ClientEndExceptions.ParameterInvalid.class)
public ResponseResult<Void> handleParameterInvalid(final ClientEndExceptions.ParameterInvalid e) {
return ResponseResult.validateFailed(e.getMessage());
}
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(value = SaTokenException.class)
public ResponseResult<Void> handleSaTokenException(SaTokenException e) {
log.warn("sa error: {}", e.getMessage());
return ResponseResult.unauthorized(e.getMessage());
}
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(ClientEndExceptions.AuthenticationFailure.class)
public ResponseResult<Void> handleAuthenticationFailure(final ClientEndExceptions.AuthenticationFailure e) {
return ResponseResult.unauthorized(e.getMessage());
}
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(ClientEndExceptions.OperationForbidden.class)
public ResponseResult<Void> handleForbiddenOperation(final ClientEndExceptions.OperationForbidden e) {
return ResponseResult.forbidden(e.getMessage());
}
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(ClientEndExceptions.ResourceNotFound.class)
public ResponseResult<Void> handleResourceNotFound(final ClientEndExceptions.ResourceNotFound e) {
return ResponseResult.notFound(e.getMessage());
}
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class, HttpMediaTypeNotSupportedException.class})
public ResponseResult<Void> handleNotSupport(final Exception e) {
return ResponseResult.notAllowed(e.getMessage());
}
@ResponseStatus(HttpStatus.REQUEST_TIMEOUT)
@ExceptionHandler(ClientEndExceptions.RequestTimeout.class)
public ResponseResult<Void> handleRequestTimeout(final ClientEndExceptions.RequestTimeout e) {
return ResponseResult.requestTimeout(e.getMessage());
}
@ResponseStatus(HttpStatus.LOCKED)
@ExceptionHandler(ClientEndExceptions.ResourceLocked.class)
public ResponseResult<Void> handleResourceLocked(final ClientEndExceptions.ResourceLocked e) {
return ResponseResult.locked(e.getMessage());
}
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
@ExceptionHandler(ClientEndException.class)
public ResponseResult<String> handleClientEndException(final ClientEndException e) {
log.warn("client end exception; code:{}, message:{}, data:{}", e.getCode(), e.getMessage(), e.getData());
return ResponseResult.failed(e.getCode(), e.getMessage(), e.getData());
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(StatusRuntimeException.class)
public ResponseResult<Void> handleStatusRuntimeException(StatusRuntimeException e) {
log.error("micro service error;", e);
return ResponseResult.failed(e.getMessage());
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ResponseResult<Void> handleException(Exception e) {
errorFixedPosition(e);
log.error("fatal error;", e);
return ResponseResult.failed(e.getMessage());
}
/**
* 定位错误发生的位置
*
* @param e 错误参数
*/
private void errorFixedPosition(Exception e) {
final var stackTrace = e.getStackTrace()[0];
final var className = stackTrace.getClassName();
final var lineNumber = stackTrace.getLineNumber();
final var methodName = stackTrace.getMethodName();
log.error("=============================错误信息如下=============================");
log.error("_> 异常定位:");
log.error("_> 类[{}] ==> 方法[{}] ==> 所在行[{}]", className, methodName, lineNumber);
if (e.getMessage() != null && !e.getMessage().isEmpty()) {
log.error("_> 错误信息:{}", e.getMessage());
}
}
}