@tanstack/ai
Version:
Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.
44 lines (43 loc) • 1.57 kB
JavaScript
//#region src/utilities/errors.ts
/**
* Best-effort extraction of a human-readable message from an unknown thrown
* value, returning `undefined` when none can be found.
*
* Used by `otelMiddleware` so error reporting stays identical across chat and
* media spans.
*/
function errorMessage(err) {
if (err instanceof Error) return err.message;
if (typeof err === "string") return err;
if (err && typeof err === "object" && "message" in err) {
const m = err.message;
if (typeof m === "string") return m;
}
}
/**
* Best-effort extraction of an error's type name (used for the `error.type`
* metric attribute), falling back to `'Error'` when no name is available.
*/
function errorTypeName(err) {
if (err instanceof Error) return err.name || "Error";
if (err && typeof err === "object" && "name" in err) {
const n = err.name;
if (typeof n === "string") return n;
}
return "Error";
}
/**
* Convert an AG-UI RUN_ERROR event to the Error shape exposed to consumers.
* Preserves the provider code and sanitized raw event when available, while
* accepting the deprecated nested error payload for backward compatibility.
*/
function runErrorEventToError(chunk) {
const error = new Error(chunk.message || chunk.error?.message || "An error occurred");
const code = chunk.code ?? chunk.error?.code;
if (code !== void 0) Object.assign(error, { code });
if (chunk.rawEvent !== void 0) Object.assign(error, { rawEvent: chunk.rawEvent });
return error;
}
//#endregion
export { errorMessage, errorTypeName, runErrorEventToError };
//# sourceMappingURL=errors.js.map