UNPKG

autotel

Version:
56 lines (55 loc) 2.07 kB
//#region src/parse-error.ts function toStatus(value) { if (typeof value === "number" && Number.isFinite(value)) return value; if (typeof value === "string") { const n = Number(value); if (Number.isFinite(n)) return n; } } function pickString(value) { return typeof value === "string" && value.length > 0 ? value : void 0; } function pickCode(value) { if (typeof value === "string" || typeof value === "number") return value; } function pickDetails(value) { if (value && typeof value === "object" && value.constructor === Object) return value; } function parseError(error) { if (error instanceof Error) { const structured = error; return { message: error.message || "An error occurred", status: toStatus(structured.status) ?? 500, why: pickString(structured.why), fix: pickString(structured.fix), link: pickString(structured.link), code: pickCode(structured.code), details: pickDetails(structured.details), raw: error }; } if (error && typeof error === "object") { const err = error; const data = err.data && typeof err.data === "object" ? err.data : void 0; const payload = (data?.data && typeof data.data === "object" ? data.data : void 0) ?? data; return { message: pickString(data?.statusText) || pickString(data?.statusMessage) || pickString(data?.message) || pickString(payload?.statusText) || pickString(payload?.statusMessage) || pickString(payload?.message) || pickString(err.message) || "An error occurred", status: toStatus(payload?.status) || toStatus(payload?.statusCode) || toStatus(err.status) || toStatus(err.statusCode) || 500, why: pickString(payload?.why) || pickString(err.why), fix: pickString(payload?.fix) || pickString(err.fix), link: pickString(payload?.link) || pickString(err.link), code: pickCode(payload?.code) || pickCode(err.code), details: pickDetails(payload?.details) || pickDetails(err.details), raw: error }; } return { message: String(error), status: 500, raw: error }; } //#endregion export { parseError }; //# sourceMappingURL=parse-error.js.map