Merge pull request #166 from FlowerCard/feature/GlobalExceptionHandler

add. 全局异常处理器增加响应状态
This commit is contained in:
zongzibinbin
2024-01-31 19:08:41 +08:00
committed by GitHub

View File

@@ -2,10 +2,12 @@ package com.abin.mallchat.common.common.exception;
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
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;
@@ -16,6 +18,7 @@ public class GlobalExceptionHandler {
/**
* validation参数校验异常
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ApiResult methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException e) {
StringBuilder errorMsg = new StringBuilder();
@@ -28,6 +31,7 @@ public class GlobalExceptionHandler {
/**
* validation参数校验异常
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = BindException.class)
public ApiResult bindException(BindException e) {
StringBuilder errorMsg = new StringBuilder();
@@ -40,6 +44,7 @@ public class GlobalExceptionHandler {
/**
* 处理空指针的异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = NullPointerException.class)
public ApiResult exceptionHandler(NullPointerException e) {
log.error("null point exceptionThe reason is: ", e);
@@ -49,6 +54,7 @@ public class GlobalExceptionHandler {
/**
* 未知异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public ApiResult systemExceptionHandler(Exception e) {
log.error("system exceptionThe reason is{}", e.getMessage(), e);
@@ -58,6 +64,7 @@ public class GlobalExceptionHandler {
/**
* 自定义校验异常(如参数校验等)
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = BusinessException.class)
public ApiResult businessExceptionHandler(BusinessException e) {
log.info("business exceptionThe reason is{}", e.getMessage(), e);
@@ -67,6 +74,7 @@ public class GlobalExceptionHandler {
/**
* http请求方式不支持
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ApiResult<Void> handleException(HttpRequestMethodNotSupportedException e) {
log.error(e.getMessage(), e);
@@ -76,6 +84,7 @@ public class GlobalExceptionHandler {
/**
* 限流异常
*/
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
@ExceptionHandler(value = FrequencyControlException.class)
public ApiResult frequencyControlExceptionHandler(FrequencyControlException e) {
log.info("frequencyControl exceptionThe reason is{}", e.getMessage(), e);