openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
103 lines (102 loc) • 4.27 kB
JavaScript
import { s as readErrorName } from "./error-coercion-D_-xJ90S.js";
import { c as isProviderRequestSizeCeilingError, d as isTimeoutErrorMessage } from "./message-patterns-Cg93oCGB.js";
//#region src/agents/failover/error.ts
const ABORT_TIMEOUT_RE = /request was aborted|request aborted/i;
/** Structured error used to carry model fallback/failover metadata across layers. */
var FailoverError = class extends Error {
constructor(message, params) {
super(message, { cause: params.cause });
this.name = "FailoverError";
this.reason = params.reason;
this.provider = params.provider;
this.model = params.model;
this.profileId = params.profileId;
this.authMode = params.authMode;
this.status = params.status;
this.code = params.code;
this.rawError = params.rawError;
this.requestSizeCeiling = isProviderRequestSizeCeilingError(params.rawError ?? message);
this.authProfileFailure = params.authProfileFailure;
this.sessionId = params.sessionId;
this.lane = params.lane;
this.suspend = params.suspend;
this.cliTimeout = params.cliTimeout;
this.timeout = params.timeout;
this.attempts = params.attempts;
this.soonestCooldownExpiry = params.soonestCooldownExpiry;
}
};
/** Return true for native or serialized failover errors. */
function isFailoverError(err) {
if (err instanceof FailoverError) return true;
return Boolean(err && typeof err === "object" && err.name === "FailoverError" && typeof err.reason === "string");
}
function findErrorProperty(err, reader, seen = /* @__PURE__ */ new Set()) {
const direct = reader(err);
if (direct !== void 0) return direct;
if (!err || typeof err !== "object") return;
if (seen.has(err)) return;
seen.add(err);
const candidate = err;
return findErrorProperty(candidate.error, reader, seen) ?? findErrorProperty(candidate.cause, reader, seen);
}
function readDirectErrorCode(err) {
if (!err || typeof err !== "object") return;
const directCode = err.code;
if (typeof directCode === "string") {
const trimmed = directCode.trim();
return trimmed ? trimmed : void 0;
}
const detailCode = err.detail?.code;
if (typeof detailCode === "string") {
const trimmed = detailCode.trim();
return trimmed ? trimmed : void 0;
}
const status = err.status;
if (typeof status !== "string" || /^\d+$/.test(status)) return;
const trimmed = status.trim();
return trimmed ? trimmed : void 0;
}
function getFailoverErrorCode(err) {
return isFailoverError(err) ? err.code : findErrorProperty(err, readDirectErrorCode);
}
function readDirectErrorMessage(err) {
if (err instanceof Error) return err.message || void 0;
if (typeof err === "string") return err || void 0;
if (typeof err === "number" || typeof err === "boolean" || typeof err === "bigint") return String(err);
if (typeof err === "symbol") return err.description ?? void 0;
if (err && typeof err === "object") {
const message = err.message;
if (typeof message === "string") return message || void 0;
}
}
function getErrorMessage(err) {
return findErrorProperty(err, readDirectErrorMessage) ?? "";
}
function hasTimeoutHint(err) {
if (!err) return false;
if (readErrorName(err) === "TimeoutError") return true;
const message = getErrorMessage(err);
return Boolean(message && isTimeoutErrorMessage(message));
}
/** Return true when an unknown error shape represents a timeout. */
function isTimeoutError(err) {
if (hasTimeoutHint(err)) return true;
if (!err || typeof err !== "object") return false;
if (readErrorName(err) !== "AbortError") return false;
const message = getErrorMessage(err);
if (message && ABORT_TIMEOUT_RE.test(message)) return true;
const cause = "cause" in err ? err.cause : void 0;
const reason = "reason" in err ? err.reason : void 0;
return hasTimeoutHint(cause) || hasTimeoutHint(reason);
}
/** Return true when an abort-signal reason is an intentional timeout; plain AbortError is a cancellation, not a timeout. */
function isSignalTimeoutReason(reason) {
try {
return readErrorName(reason) === "TimeoutError";
} catch {
return false;
}
}
//#endregion
export { isFailoverError as a, readDirectErrorCode as c, getFailoverErrorCode as i, readDirectErrorMessage as l, findErrorProperty as n, isSignalTimeoutReason as o, getErrorMessage as r, isTimeoutError as s, FailoverError as t };