自定义异常类

自定义异常类

自定义异常

package com.es.test.exception;

import com.es.test.util.ResponseCodeEnum;

/**
 * 自定义异常(CustomException)
 *
 * @author arjun
 * @date 2020/7/27 13:59
 */
public class CustomException extends RuntimeException {


    private String code;

    // 给子类用的方法
    public CustomException(ResponseCodeEnum responseCodeEnum) {
        this(responseCodeEnum.getMessage(), responseCodeEnum.getCode());
    }

    private CustomException(String message, String code) {
        super(message);
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

}

系统异常

package com.es.test.exception;

/**
 * 系统异常(SystemException)
 *
 * @author arjun
 * @date 2020/7/27 13:59
 */
public class SystemException extends RuntimeException {

    public SystemException(String msg){
        super(msg);
    }

    public SystemException() {
        super();
    }
}

code

package com.es.test.util;

/**
 * @author arjun
 * @date 2020/7/27 13:59
 */
public enum ResponseCodeEnum {

	PARAMS_FAILED("A0400", "用户请求参数错误"),

	PARAMS_NULL("A0410", "请求必填参数为空"),

	SYS_ERR("B0320", "系统资源访问异常"),

	CACHE_OVERTIME("C0230", "缓存服务超时"),

	TABLE_NOT_FIND("C0311", "表不存在"),

	FORCED_TRANSFER_ERROR("B0312", "强转出错");

	private String code;
	private String message;

	ResponseCodeEnum(String code, String message) {
		this.code = code;
		this.message = message;
	}

	public final String getCode() {
		return this.code;
	}

	public final String getMessage() {
		return this.message;
	}
}

异常控制处理器

package com.es.test.exception;

import com.es.test.exception.CustomException;
import com.es.test.exception.SystemException;
import com.es.test.util.R;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常控制处理器
 *
 * @author arjun
 * @date 2020/7/27 13:59
 */
@Slf4j
public class ExceptionAdvice {
    /**
     * 捕捉自定义异常
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(CustomException.class)
    public R handle(CustomException e) {
        log.error(e.getMessage());
        return R.failed(e.getMessage());
    }

    /**
     * 捕捉系统异常
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(SystemException.class)
    public R handle(SystemException e) {
        log.error(e.getMessage(),e);
        return new R(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), null);
    }

    /**
     * 捕捉404异常
     * @return
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public R handle(NoHandlerFoundException e) {
        log.error(e.getMessage(),e);
        return new R(HttpStatus.NOT_FOUND.value(),HttpStatus.NOT_FOUND.getReasonPhrase(), null);
    }

    /**
     * @param request
     * @param ex
     * @return
     */
    /*@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public R globalException(HttpServletRequest request, Throwable ex) {
        log.error(ex.toString()+ex.getMessage(),ex);
        return new R(this.getStatus(request).value(),this.getStatus(request).getReasonPhrase() , null);
    }*/


    /**
     * 获取状态码
     * @param request
     * @return
     */
    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }

    /**
     * 捕捉系统异常
     * @return
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(MyBatisSystemException.class)
    public R handle(MyBatisSystemException e) {
        log.error("全局异常信息 ex={}", e.getMessage(), e);
        return new R(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), null);
    }

    @ExceptionHandler(UncategorizedSQLException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R sqlExceptionHandler(UncategorizedSQLException e) {
        log.error("数据库SQL执行异常 ex={}", e.getMessage(), e);
        return R.failed("数据库操作异常");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值