CRM项目使用全局异常捕获实现拦截数据库出现的异常情况并提交回前端------CRM项目

package com.alatus.result;

import lombok.*;

@Getter
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
public enum CodeEnum {
    OK(200,"成功"),

    FAIL(500,"失败"),

    TOKEN_IS_EMPTY(901,"请求Token参数为空"),

    TOKEN_IS_EXPIRED(902,"Token已过期"),

    TOKEN_IS_ERROR(903,"Token有误"),

    TOKEN_IS_NONE_MATCH(904,"Token信息不合法"),

    USER_LOG_OUT(200,"退出成功"),

    DATA_ACCESS_EXCEPTION(500,"数据库操作失败");

//    结果码
    private int code;
//    结果信息
    @NonNull
    private String msg;
}
package com.alatus.result;

import lombok.*;

@Getter
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
public enum CodeEnum {
    OK(200,"成功"),

    FAIL(500,"失败"),

    TOKEN_IS_EMPTY(901,"请求Token参数为空"),

    TOKEN_IS_EXPIRED(902,"Token已过期"),

    TOKEN_IS_ERROR(903,"Token有误"),

    TOKEN_IS_NONE_MATCH(904,"Token信息不合法"),

    USER_LOG_OUT(200,"退出成功"),

    DATA_ACCESS_EXCEPTION(500,"数据库操作失败");

//    结果码
    private int code;
//    结果信息
    @NonNull
    private String msg;
}
package com.alatus.config.handler;

import com.alatus.result.CodeEnum;
import com.alatus.result.R;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
//拦截标注了@RestController的所有controller的方法
@RestControllerAdvice
public class GlobalExceptionHandler {

    DataIntegrityViolationException e;

    @ExceptionHandler
    public R handlerException(Exception e) {
        //打出异常信息
        e.printStackTrace();
        return R.FAIL(e.getMessage());
    }

    @ExceptionHandler
    public R handlerException(AccessDeniedException e) {
        e.printStackTrace();
        return R.FAIL("权限不足");
    }

//    数据库的异常
    @ExceptionHandler(value = DataAccessException.class)
    public R handlerSQLException(DataAccessException e) {
        e.printStackTrace();
        return R.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
    }
}
package com.alatus.config.handler;

import com.alatus.result.CodeEnum;
import com.alatus.result.R;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
//拦截标注了@RestController的所有controller的方法
@RestControllerAdvice
public class GlobalExceptionHandler {

    DataIntegrityViolationException e;

    @ExceptionHandler
    public R handlerException(Exception e) {
        //打出异常信息
        e.printStackTrace();
        return R.FAIL(e.getMessage());
    }

    @ExceptionHandler
    public R handlerException(AccessDeniedException e) {
        e.printStackTrace();
        return R.FAIL("权限不足");
    }

//    数据库的异常
    @ExceptionHandler(value = DataAccessException.class)
    public R handlerSQLException(DataAccessException e) {
        e.printStackTrace();
        return R.FAIL(CodeEnum.DATA_ACCESS_EXCEPTION);
    }
}

你可能感兴趣的:(CRM项目,#,Spring-Boot框架,#,Mybatis框架,java,spring,boot,spring,后端,java-ee,mybatis,mysql)