Skip to content

Commit

Permalink
Merge pull request #166 from FlowerCard/feature/GlobalExceptionHandler
Browse files Browse the repository at this point in the history
add. 全局异常处理器增加响应状态
  • Loading branch information
zongzibinbin authored Jan 31, 2024
2 parents 907e417 + c0096e6 commit f256332
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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;


Expand All @@ -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();
Expand All @@ -28,6 +31,7 @@ public ApiResult methodArgumentNotValidExceptionExceptionHandler(MethodArgumentN
/**
* validation参数校验异常
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = BindException.class)
public ApiResult bindException(BindException e) {
StringBuilder errorMsg = new StringBuilder();
Expand All @@ -40,6 +44,7 @@ public ApiResult bindException(BindException e) {
/**
* 处理空指针的异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = NullPointerException.class)
public ApiResult exceptionHandler(NullPointerException e) {
log.error("null point exception!The reason is: ", e);
Expand All @@ -49,6 +54,7 @@ public ApiResult exceptionHandler(NullPointerException e) {
/**
* 未知异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public ApiResult systemExceptionHandler(Exception e) {
log.error("system exception!The reason is:{}", e.getMessage(), e);
Expand All @@ -58,6 +64,7 @@ public ApiResult systemExceptionHandler(Exception e) {
/**
* 自定义校验异常(如参数校验等)
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = BusinessException.class)
public ApiResult businessExceptionHandler(BusinessException e) {
log.info("business exception!The reason is:{}", e.getMessage(), e);
Expand All @@ -67,6 +74,7 @@ public ApiResult businessExceptionHandler(BusinessException e) {
/**
* http请求方式不支持
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ApiResult<Void> handleException(HttpRequestMethodNotSupportedException e) {
log.error(e.getMessage(), e);
Expand All @@ -76,6 +84,7 @@ public ApiResult<Void> handleException(HttpRequestMethodNotSupportedException e)
/**
* 限流异常
*/
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
@ExceptionHandler(value = FrequencyControlException.class)
public ApiResult frequencyControlExceptionHandler(FrequencyControlException e) {
log.info("frequencyControl exception!The reason is:{}", e.getMessage(), e);
Expand Down

0 comments on commit f256332

Please sign in to comment.