package com.changhu.common.exception;

import cn.hutool.core.util.StrUtil;
import com.changhu.common.enums.ResultCode;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Arrays;
import java.util.Objects;

/**
 * author: luozhun
 * desc: MessageException
 * createTime: 2023/8/16 17:27
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class MessageException extends RuntimeException {

    private Integer code = ResultCode.ERROR.getCode();
    private String message = ResultCode.ERROR.getMessage();
    private Object data;

    public MessageException(String errorMessage) {
        this.message = errorMessage;
    }

    public MessageException(Integer errorCode, String errorMessage) {
        this.code = errorCode;
        this.message = errorMessage;
    }

    public MessageException(Integer errorCode, String template, String... errorMessage) {
        this.code = errorCode;
        this.message = StrUtil.format(template, Arrays.stream(errorMessage).toArray());
    }

    public MessageException(String template, String... errorMessage) {
        this.message = StrUtil.format(template, Arrays.stream(errorMessage).toArray());
    }

    public MessageException(ResultCode resultCode, String errorMessage) {
        this.code = resultCode.getCode();
        this.message = errorMessage;
    }

    public MessageException(ResultCode resultCode, Object data, String errorMessage) {
        this.code = resultCode.getCode();
        this.message = errorMessage;
        this.data = data;
    }

    public MessageException(ResultCode resultCode, Object data) {
        this.code = resultCode.getCode();
        this.data = data;
    }

    public MessageException(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    public MessageException() {

    }

    @Override
    public String toString() {
        return StrUtil.format("业务错误：错误代码：{}，错误内容：{}", Objects.isNull(this.code) ? 500 : this.code, this.message);
    }
}
