io.github.firefang.power.exception.handler.GlobalExcptionHandler Maven / Gradle / Ivy
package io.github.firefang.power.exception.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import io.github.firefang.power.exception.BadRequestException;
import io.github.firefang.power.exception.BusinessException;
import io.github.firefang.power.exception.NoPermissionException;
import io.github.firefang.power.exception.UnAuthorizedException;
import io.github.firefang.power.web.CommonResponse;
/**
* 全局异常处理器
*
* @author xinufo
*
*/
@RestControllerAdvice
public class GlobalExcptionHandler extends ResponseEntityExceptionHandler {
protected static final Logger LOG = LoggerFactory.getLogger(GlobalExcptionHandler.class);
/**
* 处理认证失败异常
*
* @param ex 异常信息
* @return 响应信息
*/
@ExceptionHandler(UnAuthorizedException.class)
public ResponseEntity> handle(UnAuthorizedException ex) {
return ExceptionHandleUtil.handle(ex);
}
/**
* 处理无权操作异常
*
* @param ex 异常信息
* @return 响应信息
*/
@ExceptionHandler(NoPermissionException.class)
public ResponseEntity handle(NoPermissionException ex) {
return ExceptionHandleUtil.handle(ex);
}
/**
* 处理业务异常
*
* @param ex 异常信息
* @return 响应信息
*/
@ExceptionHandler(BusinessException.class)
public ResponseEntity> handleBusinessException(BusinessException ex) {
return ExceptionHandleUtil.handle(ex, LOG);
}
/**
* 处理无效请求异常
*
* @param ex 异常信息
* @param request 请求信息
* @return 响应信息
*/
@ExceptionHandler(BadRequestException.class)
public ResponseEntity> handleBadRequestException(BadRequestException ex, WebRequest request) {
return ExceptionHandleUtil.handle(ex);
}
/**
* 处理未知异常
*
* @param ex 异常信息
* @return 响应信息
*/
@ExceptionHandler(Exception.class)
public ResponseEntity> handle(Exception ex) {
return ExceptionHandleUtil.handleUnknownException(ex, LOG);
}
@Override
protected ResponseEntity
© 2015 - 2024 Weber Informatics LLC | Privacy Policy