package com.hsquirrel.zmice.web.config;
import com.hsquirrel.zmice.common.core.constant.HttpStatus;
import com.hsquirrel.zmice.common.core.domain.AjaxResult;
import com.hsquirrel.zmice.common.core.exception.RepeatSubmitException;
import com.hsquirrel.zmice.common.core.exception.ServiceException;
import com.hsquirrel.zmice.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;
/**
* 全局异常处理器
*
* @author 曹波涛
*/
@Slf4j
@Configuration
@RestControllerAdvice
public class GlobalExceptionHandler {
@PostConstruct
private void init() {
log.info("init the GlobalExceptionHandler...");
}
/**
* SQL异常
*
* @return
*/
@ExceptionHandler(SQLException.class)
public AjaxResult handleSqlException(SQLException e) {
log.error("SQL异常", e);
return AjaxResult.error("系统服务DAO异常,请联系管理员处理。");
}
/**
* 权限校验异常
*/
@ExceptionHandler(AccessDeniedException.class)
public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return AjaxResult.error(HttpStatus.FORBIDDEN, "HTTP 403 错误 - 被禁止(Forbidden) 当前身份无权访问该资源");
}
/**
* 请求方式不支持异常
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
HttpServletRequest request) {
log.error("请求地址'{}',不支持'{}'请求", request.getRequestURI(), e.getMethod());
return AjaxResult.error(e.getMessage());
}
/**
* 业务异常
*/
@ExceptionHandler(ServiceException.class)
public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
log.warn(e.getMessage(), e);
Integer code = e.getCode();
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
}
/**
* 拦截未知的运行时异常
*/
@ExceptionHandler(RuntimeException.class)
public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
log.error("请求地址'{}',发生未知异常.", request.getRequestURI(), e);
return AjaxResult.error(e.getMessage());
}
/**
* 系统异常
*/
@ExceptionHandler(Exception.class)
public AjaxResult handleException(Exception e, HttpServletRequest request) {
log.error("请求地址'{}',发生系统异常.", request.getRequestURI(), e);
return AjaxResult.error(e.getMessage());
}
/**
* 参数缺失验证异常
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public AjaxResult handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
log.warn(e.getMessage());
return AjaxResult.error(e.getMessage());
}
/**
* 自定义验证异常
*/
@ExceptionHandler(BindException.class)
public AjaxResult handleBindException(BindException e) {
log.warn(e.getMessage());
String message = e.getAllErrors().get(0).getDefaultMessage();
return AjaxResult.error(message);
}
/**
* 自定义验证异常
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public AjaxResult handleExceptionHandler(MethodArgumentNotValidException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
FieldError error = fieldErrors.get(0);
String msg = error.getDefaultMessage();
log.warn("参数校验异常:{}", e.getMessage());
return AjaxResult.error(msg);
}
/**
* 限流
*
* @param e
* @return
*/
@ExceptionHandler(RepeatSubmitException.class)
public AjaxResult handleRepeatSubmitException(RepeatSubmitException e) {
log.debug("重复提交请求限流:{}", e.getMessage());
return AjaxResult.error(e.getMessage());
}
}
03-24
1027
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-03
5505
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交