package com.ff.base.exception.base;

import com.ff.base.enums.ErrorCode;
import com.ff.base.utils.MessageUtils;
import com.ff.base.utils.StringUtils;
import lombok.Getter;
import org.springframework.lang.Nullable;

/**
 * 基础异常
 *
 * @author ff
 */
@Getter
public class ApiException extends RuntimeException {
    private static final long serialVersionUID = 1L;


    /**
     * 错误码
     */
    private Integer code;


    /**
     * 错误消息
     */
    private String message;

    public ApiException(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
    public ApiException(Integer code) {
        this.code = code;
        this.message = ErrorCode.findByCode(code);
    }

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

    /**
     * 非空
     *
     * @param object 对象
     * @param code   代码
     */
    public static void notNull(@Nullable Object object, Integer code) {
        if (object == null) {
            throw new ApiException(code);
        }
    }
}
