package com.neptune.plugin;

import com.getcapacitor.JSObject;
import com.neptune.utils.MyLog;
import com.pax.dal.exceptions.AGeneralException;

public class NeptuneException extends Exception {

    private String desc;
    private NeptuneErrors code;

    AGeneralException generalException;

    public NeptuneException() {
        this.code = NeptuneErrors.NEPTUNE_ERR_OK;
        this.desc = "";
        this.generalException = null;
    }

    public NeptuneException(NeptuneErrors error) {
        this.desc = error.desc();
        this.code = error;
        this.generalException = null;
        MyLog.logE(this.toString());
    }

    public NeptuneException(NeptuneErrors c, String error) {
        this.code = c;
        this.desc = error;
        this.generalException = null;
        MyLog.logE(this.toString());
    }

    public NeptuneException(NeptuneErrors c, String error, AGeneralException gException) {
        this.code = c;
        this.desc = error;
        this.generalException = gException;
        MyLog.logE(this.toString());
    }

    @Override
    public String toString() {
        return "NeptuneException{"
                + "code=" + code
                + ", desc=" + desc +
                '}';
    }

    public JSObject toJSON() {
        JSObject jo = new JSObject();
        jo.put("code", this.code);
        jo.put("desc", this.desc);

        if (this.generalException != null) {
            JSObject gjo = new JSObject();
            gjo.put("module", this.generalException.getErrModule());
            gjo.put("code", this.generalException.getErrCode());
            gjo.put("desc", this.generalException.getErrMsg());
            jo.put("exception", gjo);
        }
        return jo;
    }
}
