package com.qqchen.deploy.backend.framework.exception;

import com.qqchen.deploy.backend.framework.enums.ResponseCode;
import com.qqchen.deploy.backend.framework.utils.MessageUtils;
import lombok.Getter;

@Getter
public abstract class BaseException extends RuntimeException {

    private final ResponseCode errorCode;

    private final Object[] args;

    protected BaseException(ResponseCode errorCode) {
        this(errorCode, null, null);
    }

    protected BaseException(ResponseCode errorCode, Object[] args) {
        this(errorCode, args, null);
    }
    
    protected BaseException(ResponseCode errorCode, Throwable cause) {
        this(errorCode, null, cause);
    }

    protected BaseException(ResponseCode errorCode, Object[] args, Throwable cause) {
        super(buildMessage(errorCode, args), cause);
        this.errorCode = errorCode;
        this.args = args;
    }

    private static String buildMessage(ResponseCode errorCode, Object[] args) {
        if (errorCode == null) {
            return null;
        }
        if (args == null || args.length == 0) {
            return MessageUtils.getMessage(errorCode.getMessageKey());
        }
        return MessageUtils.getMessage(errorCode.getMessageKey(), args);
    }
}