All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zznote.basecommon.common.handler.BadRequestExceptionHandler Maven / Gradle / Ivy

The newest version!
package com.zznote.basecommon.common.handler;

import cn.dev33.satoken.exception.DisableLoginException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.zznote.basecommon.common.exception.ServiceException;
import com.zznote.basecommon.common.exception.UserException;
import com.zznote.basecommon.common.util.Rm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class BadRequestExceptionHandler {

    /**
     * 未登陆异常
     */
    @ExceptionHandler(NotLoginException.class)
    public Rm handleNotLoginException(NotLoginException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.info("请求地址'{}',未登陆异常'{}'", requestURI, e.getMessage());
        return Rm.unAuth(e.getMessage());
    }

    /**
     * 登陆异常
     */
    @ExceptionHandler(UserException.class)
    public Rm handleUserException(UserException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.info("请求地址:{},登陆校验异常:{}", requestURI, e.getMessage());
        return Rm.error(e.getMessage());
    }

    /**
     * 角色异常
     */
    @ExceptionHandler(NotRoleException.class)
    public Rm handleNotRoleException(NotRoleException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.info("请求地址'{}', 角色校验失败'{}'", requestURI, e.getMessage());
        return Rm.error(e.getMessage());
    }

    /**
     * 权限异常
     */
    @ExceptionHandler(NotPermissionException.class)
    public Rm handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.info("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage());
        return Rm.error(e.getMessage());
    }

    /**
     * 封禁异常
     */
    @ExceptionHandler(DisableLoginException.class)
    public Rm handleDisableLoginException(DisableLoginException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.info("请求地址'{}',账号校验失败'{}'", requestURI, e.getMessage());
        return Rm.error(e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Rm handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生未知异常.", requestURI, e);
        return Rm.error(e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public Rm handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return Rm.error(e.getMessage());
    }


    /**
     *  校验错误拦截处理
     *
     * @param e 错误信息集合
     * @return 错误信息
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Rm validationBodyException(MethodArgumentNotValidException e,HttpServletRequest request){
        String requestURI = request.getRequestURI();
        BindingResult result = e.getBindingResult();
        log.info("请求地址:{},格式校验错误:{}", requestURI, result.getFieldError().getDefaultMessage());
        return Rm.error(result.getFieldError().getDefaultMessage());
    }

    /**
     * 参数类型转换错误
     *
     * @param e 错误
     * @return 错误信息
     */
    @ExceptionHandler(HttpMessageConversionException.class)
    public Rm parameterTypeException(HttpMessageConversionException e,HttpServletRequest request){
        String requestURI = request.getRequestURI();
        log.info("请求地址:{},参数类型转换错误:{}", requestURI, e.getMessage());
        return Rm.error("类型转换错误");
    }


    /**
     * 参数类型转换错误
     *
     * @param e 错误
     * @return 错误信息
     */
    @ExceptionHandler(ServiceException.class)
    public Rm handleServiceException(ServiceException e,HttpServletRequest request){
        String requestURI = request.getRequestURI();
        log.info("请求地址:{},业务错误:{}", requestURI, e.getMessage());
        return Rm.error(e.getMessage());
    }

    @ExceptionHandler(BindException.class)
    public Rm handleValidation(BindException e,HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        for (ObjectError error : e.getBindingResult().getAllErrors()) {
            log.info("请求地址:{},业务错误:{}", requestURI, error.getDefaultMessage());
            return Rm.error(error.getDefaultMessage());
        }
        log.info("请求地址:{},业务错误:{}", requestURI, e.getFieldError().getDefaultMessage());
        return Rm.error("请求参数有误");
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy