UNPKG

@lifi/composer-sdk

Version:

Public Composer SDK for building and submitting flows

117 lines 4.16 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var errors_exports = {}; __export(errors_exports, { ComposeError: () => ComposeError, errorFromHttpResponse: () => errorFromHttpResponse, isComposeError: () => isComposeError, isComposePreparationError: () => isComposePreparationError }); module.exports = __toCommonJS(errors_exports); var import_responseSchemas = require("./responseSchemas.js"); class ComposeError extends Error { name = "ComposeError"; /** Machine-readable error category. */ code; /** HTTP status code, when the error originated from an HTTP response. */ status; /** The request URL that produced the error. */ url; /** Server-provided error kind for finer-grained classification. */ kind; /** JSON-pointer path to the field that caused a validation error. */ path; /** * Simulation revert diagnostics attached to `simulation_revert` errors. * Contains the raw error bytes and decoded error candidates when the * backend can parse the revert reason. */ details; /** * The prepared ops that failed, attached to `preparation_error` errors. * Each entry carries the `callId` of the failing node so callers can drop * the unroutable legs and resubmit a smaller flow. */ failedOps; /** * The `callId`s of the prepared ops that succeeded, attached to * `preparation_error` errors alongside {@link ComposeError.failedOps}. */ succeededOps; constructor(code, message, options) { super(message, { cause: options?.cause }); this.code = code; this.status = options?.status; this.url = options?.url; this.kind = options?.kind; this.path = options?.path; this.details = options?.details; this.failedOps = options?.failedOps; this.succeededOps = options?.succeededOps; } } const isComposeError = (e) => e instanceof ComposeError || e instanceof Error && e.name === "ComposeError" && "code" in e; const isComposePreparationError = (e) => isComposeError(e) && e.kind === "preparation_error" && Array.isArray(e.failedOps) && Array.isArray(e.succeededOps); const STATUS_TO_CODE = /* @__PURE__ */ new Map([ [400, "VALIDATION_ERROR"], [401, "UNAUTHENTICATED"], [403, "FORBIDDEN"], [404, "NOT_FOUND"], [422, "VALIDATION_ERROR"], [429, "RATE_LIMITED"] ]); const tryParseErrorBody = (body) => { let json; try { json = JSON.parse(body); } catch { return null; } return (0, import_responseSchemas.parseServerErrorBody)(json); }; const errorFromHttpResponse = (status, body, url) => { const parsed = tryParseErrorBody(body); const serverError = parsed?.error; return new ComposeError( STATUS_TO_CODE.get(status) ?? (status >= 500 ? "SERVER_ERROR" : "UNKNOWN_ERROR"), (serverError?.message ?? body) || `HTTP ${status}`, { status, url, kind: serverError?.kind, path: serverError?.path, details: serverError?.details, // The wire schema types `kind` as a bare string; the server only ever // reports generic kinds for individual ops. failedOps: serverError?.failedOps?.map((op) => ({ ...op, kind: op.kind })), succeededOps: serverError?.succeededOps } ); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ComposeError, errorFromHttpResponse, isComposeError, isComposePreparationError }); //# sourceMappingURL=errors.cjs.map