package com.cmvr.common.exception;

import cn.hutool.core.util.StrUtil;

/**
 * 全局自定义业务异常
 */
public class GlobalException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    /** 前端展示提示信息 */
    private String message;
    /** 后端详细错误信息(数据库/原始异常详情) */
    private String detailMessage;

    // 空构造，序列化使用
    public GlobalException() {
    }

    // 单纯提示信息
    public GlobalException(String message) {
        this.message = message;
    }

    // 占位符格式化提示
    public GlobalException(String template, Object... params) {
        this.message = StrUtil.format(template, params);
    }

    // 【重点：支持 提示+原始异常】对应 throw new GlobalException("任务执行失败", e);
    public GlobalException(String message, Throwable cause) {
        super(cause); // 绑定原始异常栈，日志可打全堆栈
        this.message = message;
        if (cause != null) {
            this.detailMessage = cause.getMessage();
        }
    }

    public String getDetailMessage() {
        return detailMessage;
    }

    public GlobalException setDetailMessage(String detailMessage) {
        this.detailMessage = detailMessage;
        return this;
    }

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

    public GlobalException setMessage(String message) {
        this.message = message;
        return this;
    }
}