@tanstack/ai
Version:
Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.
55 lines (54 loc) • 1.73 kB
JavaScript
const ABORT_ERROR_NAMES = /* @__PURE__ */ new Set([
"AbortError",
"APIUserAbortError",
"RequestAbortedError"
]);
function normalizeCode(codeField) {
if (typeof codeField === "string") return codeField;
if (typeof codeField === "number" && Number.isFinite(codeField)) {
return String(codeField);
}
return void 0;
}
function toRunErrorPayload(error, fallbackMessage = "Unknown error occurred") {
if (error && typeof error === "object") {
const name = error.name;
if (typeof name === "string" && ABORT_ERROR_NAMES.has(name)) {
return { message: "Request aborted", code: "aborted" };
}
}
if (error instanceof Error) {
const codeField = error.code;
return {
message: error.message || fallbackMessage,
code: normalizeCode(codeField)
};
}
if (typeof error === "object" && error !== null) {
const messageField = error.message;
const codeField = error.code;
return {
message: typeof messageField === "string" && messageField.length > 0 ? messageField : fallbackMessage,
code: normalizeCode(codeField)
};
}
if (typeof error === "string" && error.length > 0) {
return { message: error, code: void 0 };
}
return { message: fallbackMessage, code: void 0 };
}
function toRunErrorRawEvent(error) {
if (!error || typeof error !== "object") return void 0;
const e = error;
if (e.rawEvent !== void 0 && e.rawEvent !== null) return e.rawEvent;
if (e.error !== void 0 && e.error !== null && typeof e.error === "object") {
return e.error;
}
if (e.metadata !== void 0 && e.metadata !== null) return e.metadata;
return void 0;
}
export {
toRunErrorPayload,
toRunErrorRawEvent
};
//# sourceMappingURL=error-payload.js.map