package com.storlead.framework.web.exception;

import com.storlead.framework.web.message.MsgCode;
import com.storlead.framework.web.message.TopMsgCode;

public class TopException extends RuntimeException{
    private static final long serialVersionUID = 3167686194134867412L;
    private final MsgCode code;

    public TopException(MsgCode code) {
        super(code.message(), null, false, false);
        this.code = code;
    }

    public TopException(String message) {
        super(message);
        this.code = TopMsgCode.OTHER_ERROR;
    }

    public TopException(MsgCode code, String message) {
        super(message, null, false, false);
        this.code = code;
    }

    public TopException(MsgCode code, String message, Throwable cause) {
        super(message, cause, cause != null, cause != null);
        this.code = code;
    }

    public TopException(MsgCode code, Throwable cause) {
        super(cause);
        this.code = code;
    }

    public MsgCode getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        String msg = super.getMessage();
        if (msg == null || msg.trim().length() == 0) {
            msg = code.message();
        }
        return msg;
    }
}
