@mastra/core
Version:
221 lines (220 loc) • 7.12 kB
JavaScript
//#region ../_internal-core/dist/error/index.js
/**
* Safely converts an object to a string representation.
* Uses JSON.stringify first, but falls back to String() if:
* - JSON.stringify fails (e.g., circular references)
* - JSON.stringify returns "{}" (e.g., Error objects with no enumerable properties)
*/
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);
}
}
/**
* Safely converts an unknown error to an Error instance.
*/
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;
}
const DEFAULT_MAX_DEPTH = 5;
/**
* Adds a toJSON method to an Error instance for proper serialization.
* Ensures that message, name, cause, and custom properties are all serialized.
* Stack is only included in JSON output if serializeStack is true.
* Recursively adds toJSON to the cause chain for proper nested error serialization.
*/
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
});
}
let ErrorDomain = /* @__PURE__ */ function(ErrorDomain) {
ErrorDomain["TOOL"] = "TOOL";
ErrorDomain["AGENT"] = "AGENT";
ErrorDomain["MCP"] = "MCP";
ErrorDomain["AGENT_NETWORK"] = "AGENT_NETWORK";
ErrorDomain["MASTRA_SERVER"] = "MASTRA_SERVER";
ErrorDomain["MASTRA_OBSERVABILITY"] = "MASTRA_OBSERVABILITY";
ErrorDomain["MASTRA_WORKFLOW"] = "MASTRA_WORKFLOW";
ErrorDomain["MASTRA_VOICE"] = "MASTRA_VOICE";
ErrorDomain["MASTRA_VECTOR"] = "MASTRA_VECTOR";
ErrorDomain["MASTRA_MEMORY"] = "MASTRA_MEMORY";
ErrorDomain["LLM"] = "LLM";
ErrorDomain["EVAL"] = "EVAL";
ErrorDomain["SCORER"] = "SCORER";
ErrorDomain["A2A"] = "A2A";
ErrorDomain["MASTRA_INSTANCE"] = "MASTRA_INSTANCE";
ErrorDomain["MASTRA"] = "MASTRA";
ErrorDomain["DEPLOYER"] = "DEPLOYER";
ErrorDomain["STORAGE"] = "STORAGE";
ErrorDomain["MODEL_ROUTER"] = "MODEL_ROUTER";
return ErrorDomain;
}({});
let ErrorCategory = /* @__PURE__ */ function(ErrorCategory) {
ErrorCategory["UNKNOWN"] = "UNKNOWN";
ErrorCategory["USER"] = "USER";
ErrorCategory["SYSTEM"] = "SYSTEM";
ErrorCategory["THIRD_PARTY"] = "THIRD_PARTY";
return ErrorCategory;
}({});
/**
* Base error class for the Mastra ecosystem.
* It standardizes error reporting and can be extended for more specific error types.
*/
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 {};
/** Permanent workflow step failure — bypasses workflow retryConfig. */
var MastraNonRetryableError = class extends Error {
isNonRetryable = true;
constructor(message, options) {
super(message, options);
this.name = "MastraNonRetryableError";
Object.setPrototypeOf(this, new.target.prototype);
}
};
//#endregion
Object.defineProperty(exports, "ErrorCategory", {
enumerable: true,
get: function() {
return ErrorCategory;
}
});
Object.defineProperty(exports, "ErrorDomain", {
enumerable: true,
get: function() {
return ErrorDomain;
}
});
Object.defineProperty(exports, "MastraBaseError", {
enumerable: true,
get: function() {
return MastraBaseError;
}
});
Object.defineProperty(exports, "MastraError", {
enumerable: true,
get: function() {
return MastraError;
}
});
Object.defineProperty(exports, "MastraNonRetryableError", {
enumerable: true,
get: function() {
return MastraNonRetryableError;
}
});
Object.defineProperty(exports, "getErrorFromUnknown", {
enumerable: true,
get: function() {
return getErrorFromUnknown;
}
});
Object.defineProperty(exports, "safeParseErrorObject", {
enumerable: true,
get: function() {
return safeParseErrorObject;
}
});
//# sourceMappingURL=error-B-e62x-A.cjs.map