业务异常的简单封装(支持对异常信息的格式化)

package com.lix.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 业务异常的简单封装,支持对异常信息的格式化。根据业务需求对构造器、方法等补全,这里只提供几个简单示例
 * @author lix
 * @Date 2020/4/8
 */
public class BizBaseException extends RuntimeException {


    private static final String REGX_PLACEHOLDER = "\\{\\}";
    private static final String COMMON_ERROR_CODE = "9999";
    /**
     * 匹配正则占位符{}
     */
    private static final Pattern SIGN = Pattern.compile(REGX_PLACEHOLDER);
    /**
     * 错误代码
     */
    private String code;
    /**
     * 错误描述
     */
    private String desc;
    /**
     * 补位参数
     */
    private transient Object[] args;
    /**
     * 异常对象
     */
    private transient Throwable throwable;


    public static BizBaseException format(String message, Object... args){
        return new BizBaseException(COMMON_ERROR_CODE, message, args);
    }

    public static BizBaseException create(String code, String message, Object... args){
        return new BizBaseException(code, message, args);
    }


    public BizBaseException(String code, String desc, Object... args) {
        super(formatMessage(desc, args));
        this.args = args;
        this.desc = desc;
        this.code = code;
    }

    public BizBaseException(Throwable cause, String code, String desc, Object... args) {
        super(formatMessage(desc, args), cause);
        this.desc = desc;
        this.args = args;
        this.code = code;
    }

    public static String formatMessage(String message, Object[] args) {
        if (message == null || message.length() < 1) return "unknown exception";
        if (args == null || args.length < 1) {
            return message.contains("{}") ? message.replaceAll(REGX_PLACEHOLDER, "") : message;
        }
        try {
            Matcher matcher = SIGN.matcher(message);
            StringBuffer stringBuffer = new StringBuffer(64);
            int i = 0;
            while (matcher.find()) {
                matcher.appendReplacement(stringBuffer, args[i++].toString());
            }
            matcher.appendTail(stringBuffer);
            return stringBuffer.toString();
        } catch (Exception ex) {
            return message + ",errorArgs:" + args;
        }
    }

    public String getCode() {
        return code;
    }

    public Object[] getArgs() {
        return args;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public String getDesc() {
        return desc;
    }

    public static void main(String[] args) {
        // Exception in thread "main" com.lix.test.BizBaseException: 出现了异常,我需要提醒1是参数1,然后提醒2是参数2
        throw BizBaseException.format("出现了异常,我需要提醒1是{},然后提醒2是{}", "参数1", "参数2");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值