@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
91 lines • 3 kB
JavaScript
;
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
});
module.exports = __toCommonJS(errors_exports);
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;
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;
}
}
const isComposeError = (e) => e instanceof ComposeError || e instanceof Error && e.name === "ComposeError" && "code" in e;
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) => {
try {
return JSON.parse(body);
} catch {
return null;
}
};
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
}
);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ComposeError,
errorFromHttpResponse,
isComposeError
});
//# sourceMappingURL=errors.cjs.map