mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-13 21:53:41 +08:00
add. 全局异常处理器增加响应状态
This commit is contained in:
@@ -2,10 +2,12 @@ package com.abin.mallchat.common.common.exception;
|
|||||||
|
|
||||||
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
|
||||||
@@ -16,6 +18,7 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* validation参数校验异常
|
* validation参数校验异常
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||||
public ApiResult methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException e) {
|
public ApiResult methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException e) {
|
||||||
StringBuilder errorMsg = new StringBuilder();
|
StringBuilder errorMsg = new StringBuilder();
|
||||||
@@ -28,6 +31,7 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* validation参数校验异常
|
* validation参数校验异常
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(value = BindException.class)
|
@ExceptionHandler(value = BindException.class)
|
||||||
public ApiResult bindException(BindException e) {
|
public ApiResult bindException(BindException e) {
|
||||||
StringBuilder errorMsg = new StringBuilder();
|
StringBuilder errorMsg = new StringBuilder();
|
||||||
@@ -40,6 +44,7 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* 处理空指针的异常
|
* 处理空指针的异常
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
@ExceptionHandler(value = NullPointerException.class)
|
@ExceptionHandler(value = NullPointerException.class)
|
||||||
public ApiResult exceptionHandler(NullPointerException e) {
|
public ApiResult exceptionHandler(NullPointerException e) {
|
||||||
log.error("null point exception!The reason is: ", e);
|
log.error("null point exception!The reason is: ", e);
|
||||||
@@ -49,6 +54,7 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* 未知异常
|
* 未知异常
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
@ExceptionHandler(value = Exception.class)
|
@ExceptionHandler(value = Exception.class)
|
||||||
public ApiResult systemExceptionHandler(Exception e) {
|
public ApiResult systemExceptionHandler(Exception e) {
|
||||||
log.error("system exception!The reason is:{}", e.getMessage(), e);
|
log.error("system exception!The reason is:{}", e.getMessage(), e);
|
||||||
@@ -58,6 +64,7 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* 自定义校验异常(如参数校验等)
|
* 自定义校验异常(如参数校验等)
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(value = BusinessException.class)
|
@ExceptionHandler(value = BusinessException.class)
|
||||||
public ApiResult businessExceptionHandler(BusinessException e) {
|
public ApiResult businessExceptionHandler(BusinessException e) {
|
||||||
log.info("business exception!The reason is:{}", e.getMessage(), e);
|
log.info("business exception!The reason is:{}", e.getMessage(), e);
|
||||||
@@ -67,6 +74,7 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* http请求方式不支持
|
* http请求方式不支持
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
||||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||||
public ApiResult<Void> handleException(HttpRequestMethodNotSupportedException e) {
|
public ApiResult<Void> handleException(HttpRequestMethodNotSupportedException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
@@ -76,6 +84,7 @@ public class GlobalExceptionHandler {
|
|||||||
/**
|
/**
|
||||||
* 限流异常
|
* 限流异常
|
||||||
*/
|
*/
|
||||||
|
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
|
||||||
@ExceptionHandler(value = FrequencyControlException.class)
|
@ExceptionHandler(value = FrequencyControlException.class)
|
||||||
public ApiResult frequencyControlExceptionHandler(FrequencyControlException e) {
|
public ApiResult frequencyControlExceptionHandler(FrequencyControlException e) {
|
||||||
log.info("frequencyControl exception!The reason is:{}", e.getMessage(), e);
|
log.info("frequencyControl exception!The reason is:{}", e.getMessage(), e);
|
||||||
|
|||||||
Reference in New Issue
Block a user