package com.njzscloud.common.core.ex;

import cn.hutool.core.map.MapUtil;
import com.njzscloud.common.core.jackson.Jackson;

/**
 * 系统异常
 */
public abstract class SysThrowable extends RuntimeException {

    /**
     * 希望值
     *
     * @see ExpectData
     */
    public final Object expect;

    /**
     * 异常信息(简略)
     */
    public final ExceptionMsg msg;

    /**
     * 异常信息(详细)
     */
    public final Object message;

    /**
     * 创建异常
     *
     * @param cause   源异常
     * @param expect  期望响应值
     * @param msg     异常信息（简明）
     * @param message 异常信息（详细）
     */
    protected SysThrowable(Throwable cause, Object expect, ExceptionMsg msg, Object message) {
        super(msg.msg, cause);
        this.msg = msg;
        this.message = message == null ? "" : message;
        this.expect = expect;
    }

    @Override
    public String toString() {
        return Jackson.toJsonStr(MapUtil.builder()
                .put("code", msg.code)
                .put("expect", expect)
                .put("msg", msg.msg)
                .put("message", message)
                .build());
    }
}
