refactor(exception): 重构全局异常处理器
This commit is contained in:
+20
-11
@@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package cn.odboy.framework.exception.handler;
|
package cn.odboy.framework.exception.handler;
|
||||||
|
|
||||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
||||||
import cn.odboy.framework.exception.BadRequestException;
|
import cn.odboy.framework.exception.BadRequestException;
|
||||||
import io.swagger.v3.oas.annotations.Hidden;
|
import io.swagger.v3.oas.annotations.Hidden;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -42,31 +41,41 @@ public class GlobalExceptionHandler {
|
|||||||
* 处理所有不可知的异常
|
* 处理所有不可知的异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(Throwable.class)
|
@ExceptionHandler(Throwable.class)
|
||||||
public ResponseEntity<ApiError> handleException(Throwable e) {
|
public ResponseEntity<ApiError> handleThrowable(Throwable e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error("handleThrowable", e);
|
||||||
return buildResponseEntity(ApiError.error(e.getMessage()));
|
return this.buildResponseEntity(ApiError.error(e.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理所有不可知的异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<ApiError> handleException(Exception e) {
|
||||||
|
// 打印堆栈信息
|
||||||
|
log.error("handleException", e);
|
||||||
|
return this.buildResponseEntity(ApiError.error(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BadCredentialsException
|
* BadCredentialsException
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(BadCredentialsException.class)
|
@ExceptionHandler(BadCredentialsException.class)
|
||||||
public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e) {
|
public ResponseEntity<ApiError> handleBadCredentialsException(BadCredentialsException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
||||||
log.error(message);
|
log.error(message);
|
||||||
return buildResponseEntity(ApiError.error(message));
|
return this.buildResponseEntity(ApiError.error(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理自定义异常
|
* 处理自定义异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(value = BadRequestException.class)
|
@ExceptionHandler(value = BadRequestException.class)
|
||||||
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
public ResponseEntity<ApiError> handleBadRequestException(BadRequestException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error("handleBadRequestException", e);
|
||||||
return buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
|
return this.buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,13 +84,13 @@ public class GlobalExceptionHandler {
|
|||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error("handleMethodArgumentNotValidException", e);
|
||||||
ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
|
ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
|
||||||
String message = objectError.getDefaultMessage();
|
String message = objectError.getDefaultMessage();
|
||||||
if (objectError instanceof FieldError) {
|
if (objectError instanceof FieldError) {
|
||||||
message = ((FieldError) objectError).getField() + ": " + message;
|
message = ((FieldError) objectError).getField() + ": " + message;
|
||||||
}
|
}
|
||||||
return buildResponseEntity(ApiError.error(message));
|
return this.buildResponseEntity(ApiError.error(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+20
-10
@@ -40,31 +40,41 @@ public class GlobalExceptionHandler {
|
|||||||
* 处理所有不可知的异常
|
* 处理所有不可知的异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(Throwable.class)
|
@ExceptionHandler(Throwable.class)
|
||||||
public ResponseEntity<ApiError> handleException(Throwable e) {
|
public ResponseEntity<ApiError> handleThrowable(Throwable e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error("handleThrowable", e);
|
||||||
return buildResponseEntity(ApiError.error(e.getMessage()));
|
return this.buildResponseEntity(ApiError.error(e.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理所有不可知的异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<ApiError> handleException(Exception e) {
|
||||||
|
// 打印堆栈信息
|
||||||
|
log.error("handleException", e);
|
||||||
|
return this.buildResponseEntity(ApiError.error(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BadCredentialsException
|
* BadCredentialsException
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(BadCredentialsException.class)
|
@ExceptionHandler(BadCredentialsException.class)
|
||||||
public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e) {
|
public ResponseEntity<ApiError> handleBadCredentialsException(BadCredentialsException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
||||||
log.error(message);
|
log.error(message);
|
||||||
return buildResponseEntity(ApiError.error(message));
|
return this.buildResponseEntity(ApiError.error(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理自定义异常
|
* 处理自定义异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(value = BadRequestException.class)
|
@ExceptionHandler(value = BadRequestException.class)
|
||||||
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
public ResponseEntity<ApiError> handleBadRequestException(BadRequestException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error("handleBadRequestException", e);
|
||||||
return buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
|
return this.buildResponseEntity(ApiError.error(e.getStatus(), e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -73,13 +83,13 @@ public class GlobalExceptionHandler {
|
|||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
||||||
// 打印堆栈信息
|
// 打印堆栈信息
|
||||||
log.error(ExceptionUtil.stacktraceToString(e));
|
log.error("handleMethodArgumentNotValidException", e);
|
||||||
ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
|
ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
|
||||||
String message = objectError.getDefaultMessage();
|
String message = objectError.getDefaultMessage();
|
||||||
if (objectError instanceof FieldError) {
|
if (objectError instanceof FieldError) {
|
||||||
message = ((FieldError) objectError).getField() + ": " + message;
|
message = ((FieldError) objectError).getField() + ": " + message;
|
||||||
}
|
}
|
||||||
return buildResponseEntity(ApiError.error(message));
|
return this.buildResponseEntity(ApiError.error(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user