package edu.whut.types.exception;

import edu.whut.types.enums.ResponseCode;
import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = true)
@Data
public class AppException extends RuntimeException {
    private static final long serialVersionUID = 5317680961212299217L;

    private final String code;
    private final String info;

    public AppException(String code) {
        super(code);                 // 至少给个非空 message
        this.code = code;
        this.info = null;
    }

    public AppException(ResponseCode rc) {
        super(rc.getInfo());         // 关键：作为 Throwable 的 message
        this.code = rc.getCode();
        this.info = rc.getInfo();
    }

    public AppException(String code, Throwable cause) {
        super(code, cause);          // 用父类带 cause 的构造器
        this.code = code;
        this.info = null;
    }

    public AppException(String code, String message) {
        super(message);
        this.code = code;
        this.info = message;
    }

    public AppException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.info = message;
    }

    @Override public String getMessage() {
        return (info != null) ? info : super.getMessage();
    }

    @Override public String toString() {
        return "AppException{code='" + code + "', info='" + info + "'}";
    }
}
