UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

171 lines (169 loc) 5.72 kB
// src/error/utils.ts function safeParseErrorObject(obj) { if (typeof obj !== "object" || obj === null) { return String(obj); } try { const stringified = JSON.stringify(obj); if (stringified === "{}") { return String(obj); } return stringified; } catch { return String(obj); } } function getErrorFromUnknown(unknown, options = {}) { const defaultOptions = { fallbackMessage: "Unknown error", maxDepth: 5, supportSerialization: true, serializeStack: true }; const mergedOptions = options ? { ...defaultOptions, ...options } : defaultOptions; const { fallbackMessage, maxDepth, supportSerialization, serializeStack } = mergedOptions; if (unknown && unknown instanceof Error) { if (supportSerialization) { addErrorToJSON(unknown, serializeStack, { maxDepth }); } return unknown; } let error; if (unknown && typeof unknown === "object") { const errorMessage = unknown && "message" in unknown && typeof unknown.message === "string" ? unknown.message : safeParseErrorObject(unknown); const errorCause = "cause" in unknown && unknown.cause !== void 0 ? unknown.cause instanceof Error ? unknown.cause : maxDepth > 0 ? getErrorFromUnknown(unknown.cause, { ...mergedOptions, maxDepth: maxDepth - 1 }) : void 0 : void 0; error = new Error(errorMessage, errorCause ? { cause: errorCause } : void 0); Object.assign(error, unknown); error.stack = "stack" in unknown && typeof unknown.stack === "string" ? unknown.stack : void 0; } else if (unknown && typeof unknown === "string") { error = new Error(unknown); error.stack = void 0; } else { error = new Error(fallbackMessage); } if (supportSerialization) { addErrorToJSON(error, serializeStack, { maxDepth }); } return error; } var DEFAULT_MAX_DEPTH = 5; function addErrorToJSON(error, serializeStack = true, options) { const maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH; const currentDepth = options?.currentDepth ?? 0; if (error.toJSON) { return; } if (error.cause instanceof Error && currentDepth < maxDepth) { addErrorToJSON(error.cause, serializeStack, { maxDepth, currentDepth: currentDepth + 1 }); } Object.defineProperty(error, "toJSON", { value: function() { const json = { message: this.message, name: this.name }; if (serializeStack && this.stack !== void 0) { json.stack = this.stack; } if (this.cause !== void 0) { if (this.cause instanceof Error && "toJSON" in this.cause && typeof this.cause.toJSON === "function") { json.cause = this.cause.toJSON(); } else { json.cause = this.cause; } } const errorAsAny = this; for (const key in errorAsAny) { if (errorAsAny.hasOwnProperty(key) && !(key in json) && key !== "toJSON") { json[key] = errorAsAny[key]; } } return json; }, enumerable: false, writable: true, configurable: true }); } // src/error/index.ts var ErrorDomain = /* @__PURE__ */ ((ErrorDomain2) => { ErrorDomain2["TOOL"] = "TOOL"; ErrorDomain2["AGENT"] = "AGENT"; ErrorDomain2["MCP"] = "MCP"; ErrorDomain2["AGENT_NETWORK"] = "AGENT_NETWORK"; ErrorDomain2["MASTRA_SERVER"] = "MASTRA_SERVER"; ErrorDomain2["MASTRA_OBSERVABILITY"] = "MASTRA_OBSERVABILITY"; ErrorDomain2["MASTRA_WORKFLOW"] = "MASTRA_WORKFLOW"; ErrorDomain2["MASTRA_VOICE"] = "MASTRA_VOICE"; ErrorDomain2["MASTRA_VECTOR"] = "MASTRA_VECTOR"; ErrorDomain2["MASTRA_MEMORY"] = "MASTRA_MEMORY"; ErrorDomain2["LLM"] = "LLM"; ErrorDomain2["EVAL"] = "EVAL"; ErrorDomain2["SCORER"] = "SCORER"; ErrorDomain2["A2A"] = "A2A"; ErrorDomain2["MASTRA_INSTANCE"] = "MASTRA_INSTANCE"; ErrorDomain2["MASTRA"] = "MASTRA"; ErrorDomain2["DEPLOYER"] = "DEPLOYER"; ErrorDomain2["STORAGE"] = "STORAGE"; ErrorDomain2["MODEL_ROUTER"] = "MODEL_ROUTER"; return ErrorDomain2; })(ErrorDomain || {}); var ErrorCategory = /* @__PURE__ */ ((ErrorCategory2) => { ErrorCategory2["UNKNOWN"] = "UNKNOWN"; ErrorCategory2["USER"] = "USER"; ErrorCategory2["SYSTEM"] = "SYSTEM"; ErrorCategory2["THIRD_PARTY"] = "THIRD_PARTY"; return ErrorCategory2; })(ErrorCategory || {}); var MastraBaseError = class extends Error { id; domain; category; details = {}; message; cause; constructor(errorDefinition, originalError) { const error = originalError ? getErrorFromUnknown(originalError, { serializeStack: false, fallbackMessage: "Unknown error" }) : void 0; const message = errorDefinition.text ?? error?.message ?? "Unknown error"; super(message, { cause: error }); this.id = errorDefinition.id; this.domain = errorDefinition.domain; this.category = errorDefinition.category; this.details = errorDefinition.details ?? {}; this.message = message; this.cause = error; Object.setPrototypeOf(this, new.target.prototype); } /** * Returns a structured representation of the error, useful for logging or API responses. */ toJSONDetails() { return { message: this.message, domain: this.domain, category: this.category, details: this.details }; } toJSON() { return { message: this.message, domain: this.domain, category: this.category, code: this.id, details: this.details, cause: this.cause?.toJSON?.() }; } toString() { return JSON.stringify(this.toJSON()); } }; var MastraError = class extends MastraBaseError { }; export { ErrorCategory, ErrorDomain, MastraBaseError, MastraError, getErrorFromUnknown }; //# sourceMappingURL=chunk-FJEVLHJT.js.map //# sourceMappingURL=chunk-FJEVLHJT.js.map