自定义异常

@Data
@EqualsAndHashCode(callSuper = true)
public class OpenRuntimeException extends RuntimeException {

    protected String errorCode = "Internal Server Error";
    protected String message = "No value present";
    protected int status = 500;


    public OpenRuntimeException(OpenErrorCode ssoErrorCode) {
        this(ssoErrorCode, (Object[]) null);
    }

    OpenRuntimeException(OpenErrorCode ssoErrorCode, Object... params) {
        this(ssoErrorCode, null, params);
    }

    OpenRuntimeException(OpenErrorCode ssoErrorCode, Throwable cause, Object... params) {
        this.errorCode = ssoErrorCode.name();
        this.status = ssoErrorCode.getStatus();
        this.message = ssoErrorCode.getMessage(params);
        if (cause != null) {
            initCause(cause);
        }
    }

    @Override
    public String getMessage() {
        return message;
    }
}

public interface OpenErrorCode {
    /**
     * description: 获取异常的name
     *
     * @return {@link String}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2021/4/25 9:11
     */
    String name();

    /**
     * description: 响应的状态码
     *
     * @return {@link int}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2021/4/25 9:12
     */
    int getStatus();

    /**
     * description:
     *
     * @param cause  异常对象
     * @param params 格式化参数
     * @return {@link OpenRuntimeException}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2021/4/25 9:58
     */
    default OpenRuntimeException runtimeException(Throwable cause, Object... params) {
        return new OpenRuntimeException(this, cause, params);
    }

    /**
     * description:
     *
     * @param params 格式化参数
     * @return {@link OpenRuntimeException}
     * @throws
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2021/4/25 10:00
     */
    default OpenRuntimeException runtimeException(Object... params) {
        return new OpenRuntimeException(this, params);
    }

    /**
     * description:
     *
     * @param cause 异常
     * @return {@link OpenRuntimeException}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2021/4/25 16:40
     */
    default OpenRuntimeException runtimeException(Throwable cause) {
        return new OpenRuntimeException(this, cause);
    }

    /**
     * description:
     *
     * @param params 格式化参数
     * @param locale 地区
     * @return {@link String}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2021/4/25 9:13
     */
    default String getMessage(Locale locale, Object... params) {
        String message = "NO MESSAGE!!!";
        if (locale == null) {
            locale = Locale.getDefault();
        }
        try {
            String formatter = Objects.equals("OpenExceptionEnum", this.getClass().getSimpleName()) ?
                    "META-INF.{0}" : "i18n.exception.{0}";
            ResourceBundle bundle = ResourceBundle
                    .getBundle(MessageFormat.format(formatter,
                            this.getClass().getSimpleName()), locale);
            message = bundle.getString(this.name());
            return new MessageFormat(message).format(params);
        } catch (Throwable e) {
            if (!(e instanceof MissingResourceException)) {
                e.printStackTrace();
            }
            message = Objects.isNull(params) ? message : new MessageFormat("{0}").format(params);
            return message;
        }
    }

    /**
     * description:
     *
     * @param params 格式化参数
     * @return {@link String}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2021/4/25 9:56
     */
    default String getMessage(Object... params) {
        return getMessage(Locale.getDefault(), params);
    }
}
上一篇:学习报告-动手实战-最佳应用实践之使用RDS MySQL和ECS搭建个人博客


下一篇:通过注解、切面、反射实现返回信息脱敏