java-自定义报错

报错内容 ExceptionMsg

package com.io.common.enums;

public enum ExceptionMsg {
	/**
	 * 固定项目
	 */
	SUCCESS_CODE(0,"成功"),
	SYS_REQUEST_ERROR(-1,"系统出现错误, 请联系网站管理员 !"),
	/***
	 * 系统模块异常 010000 ~ 019999
	 */
	// Token
	TOEKN_INVALID(010000,"invalid token header"),
	TOKEN_EXPIRED(010001,"token过期,请重新登录"),
	TOKEN_VERIFY_ERROR(010002,"Token验证异常"),
	TOKEN_IS_NULL(010003,"token不能为空"),
	;

    private Integer code;
    private String msg;

    ExceptionMsg(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

报错调用类 SystemException

package com.io.common.exception;


import com.io.common.enums.ExceptionMsg;
import org.apache.commons.lang3.StringUtils;

public class SystemException {

	public static void myException(ExceptionMsg msg) {
		throw new MyException(msg.getCode(), msg.getMsg());
	}
	
	public static void myException(ExceptionMsg msg, Object...  args) {
		String message = msg.getMsg();
		if(args.length > 0) {
			if(StringUtils.isBlank(message)) {
				message = msg.getMsg();
			}
			message = String.format(message, args);
		}
		throw new MyException(msg.getCode(), message);
	}
}

调用类二 MyException

package com.io.common.exception;

import java.io.Serializable;

/**
 * 自定义异常
 */
public class MyException extends RuntimeException implements Serializable {

    private static final long serialVersionUID = 1L;
    private Integer code;

    public MyException() {
    }

    public MyException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    public MyException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public Integer getCode() {
        return code;
    }

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

最终实现类

package com.io.exception;


import com.io.common.exception.MyException;
import com.io.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 */
@RestControllerAdvice
public class RRExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());
    
    /**
     * 处理自定义异常 MyException
     * R 是业务逻辑的返回类 用于 Controller 
     */
    @ExceptionHandler(MyException.class)
    public R handleRRException(MyException e) {
        R r = new R();
        r.put("code", e.getCode());
        r.put("msg", e.getMessage());

        return r;
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return R.error("数据库中已存在该记录");
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return R.error(e.getMessage());
    }

    //    R 是业务逻辑的返回类 用于 Controller 
    //    @GetMapping("/roombymonitor")
    //    @ApiOperation(value = "R 类的注解")
    //    public R note() {
    //        // 返回给前端的值  data Service层返回的值
    //        return R.ok().put("data", data);
    //    }
}

使用

  // 编写在实现方法里
  SystemException.myException(ExceptionMsg.TOKEN_IS_NULL);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值