@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
65 lines • 1.89 kB
JavaScript
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
}
);
};
export {
ComposeError,
errorFromHttpResponse,
isComposeError
};
//# sourceMappingURL=errors.js.map