UNPKG

@mozaic-io/mozaic-sdk-node

Version:

The Mozaic Node SDK enables you to pay your creators easily via the Mozaic API.

72 lines (71 loc) 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MozaicError = void 0; const axios_1 = require("axios"); class MozaicError extends Error { /** * @internal - To be used by the internal system only. * @param axiosResponse */ constructor(status, message, stack, rawError) { super(message); if (stack !== undefined) this.stack = stack; this.status = status; this.rawError = rawError; // Restore the prototype chain Object.setPrototypeOf(this, new.target.prototype); } static create(obj) { var _a; if (obj instanceof axios_1.AxiosError) { let message = ""; if (obj.errors !== undefined) { obj.errors.forEach((value, index) => { if (index > 0) message += ", "; message += value.message; }); } else { message = obj.message; } if (obj.response !== undefined) { message += ": " + obj.response.statusText; if (obj.response.data !== undefined) { if (Array.isArray(obj.response.data) == true) { obj.response.data.forEach(value => { if (value.ErrorMessage === undefined) message += JSON.stringify(value); else message += ", " + value.ErrorMessage; }); } else if (obj.response.data.errors !== undefined && obj.response.data.errors.Status !== undefined && Array.isArray(obj.response.data.errors.Status) == true) { obj.response.data.errors.Status.forEach((value, index) => { message += ", " + value; }); } else { message += ", " + obj.response.data; } } } let status = (_a = obj.status) !== null && _a !== void 0 ? _a : -1; return new MozaicError(status, message, obj.stack, obj); } else if (obj instanceof Error) { return new MozaicError(-1, obj.message, obj.stack, obj); } const response = obj; // If it is a real AxiosResponse, then these should be set at a minimum. if (response.status !== undefined && response.data !== undefined) return new MozaicError(response.status, response.data, undefined, obj); // If it's just a raw string. if (typeof obj === "string") return new MozaicError(-1, obj, undefined, obj); // Everything else! return new MozaicError(-1, JSON.stringify(obj), undefined, obj); } } exports.MozaicError = MozaicError;