UNPKG

@langchain/langgraph

Version:
1 lines 15.8 kB
{"version":3,"file":"errors.cjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["import { Command, Interrupt } from \"./constants.js\";\n\n// When editing, make sure to update the index found here:\n// https://langchain-ai.github.io/langgraphjs/troubleshooting/errors/\nexport type BaseLangGraphErrorFields = {\n lc_error_code?:\n | \"GRAPH_RECURSION_LIMIT\"\n | \"INVALID_CONCURRENT_GRAPH_UPDATE\"\n | \"INVALID_GRAPH_NODE_RETURN_VALUE\"\n | \"MISSING_CHECKPOINTER\"\n | \"MULTIPLE_SUBGRAPHS\"\n | \"UNREACHABLE_NODE\";\n};\n\n// TODO: Merge with base LangChain error class when we drop support for core@0.2.0\n/** @category Errors */\nexport class BaseLangGraphError extends Error {\n lc_error_code?: string;\n\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n let finalMessage = message ?? \"\";\n if (fields?.lc_error_code) {\n finalMessage = `${finalMessage}\\n\\nTroubleshooting URL: https://docs.langchain.com/oss/javascript/langgraph/${fields.lc_error_code}/\\n`;\n }\n super(finalMessage);\n this.lc_error_code = fields?.lc_error_code;\n }\n}\n\nexport class GraphBubbleUp extends BaseLangGraphError {\n get is_bubble_up() {\n return true;\n }\n}\n\nexport class GraphRecursionError extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"GraphRecursionError\";\n }\n\n static get unminifiable_name() {\n return \"GraphRecursionError\";\n }\n}\n\nexport class GraphValueError extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"GraphValueError\";\n }\n\n static get unminifiable_name() {\n return \"GraphValueError\";\n }\n}\n\n/**\n * Raised when a graph run exits early due to a drain request.\n *\n * This indicates the graph stopped cooperatively at a superstep boundary\n * because {@link RunControl#requestDrain} was called (e.g., in response to\n * SIGTERM). The checkpoint is saved and the run can be resumed later.\n */\nexport class GraphDrained extends GraphBubbleUp {\n reason: string;\n\n constructor(reason: string = \"shutdown\", fields?: BaseLangGraphErrorFields) {\n super(`Graph drained: ${reason}`, fields);\n this.name = \"GraphDrained\";\n this.reason = reason;\n }\n\n static get unminifiable_name() {\n return \"GraphDrained\";\n }\n}\n\nexport function isGraphDrained(e?: unknown): e is GraphDrained {\n return (\n e !== undefined && (e as Error).name === GraphDrained.unminifiable_name\n );\n}\n\nexport class GraphInterrupt extends GraphBubbleUp {\n interrupts: Interrupt[];\n\n constructor(interrupts?: Interrupt[], fields?: BaseLangGraphErrorFields) {\n super(JSON.stringify(interrupts, null, 2), fields);\n this.name = \"GraphInterrupt\";\n this.interrupts = interrupts ?? [];\n }\n\n static get unminifiable_name() {\n return \"GraphInterrupt\";\n }\n}\n\n/** Raised by a node to interrupt execution. */\nexport class NodeInterrupt extends GraphInterrupt {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n constructor(message: any, fields?: BaseLangGraphErrorFields) {\n super([{ value: message }], fields);\n this.name = \"NodeInterrupt\";\n }\n\n static get unminifiable_name() {\n return \"NodeInterrupt\";\n }\n}\n\n/**\n * Failure context passed to a node-level error handler.\n *\n * A node-level error handler is registered via\n * `StateGraph.addNode(name, fn, { errorHandler })`. The handler runs ONLY after\n * the failing node's {@link RetryPolicy} is exhausted, so retry and handling\n * stay decoupled. The handler receives the failed node's name and the thrown\n * error via a `NodeError` instance, can return a state update, and can route to\n * a recovery branch via `new Command({ goto })` (saga / compensation flows).\n *\n * @example\n * ```ts\n * import { NodeError } from \"@langchain/langgraph\";\n *\n * function handler(state: State, error: NodeError) {\n * return new Command({\n * update: { status: `recovered from ${error.node}: ${error.error.message}` },\n * goto: \"finalize\",\n * });\n * }\n * ```\n */\nexport class NodeError {\n /** Name of the node whose execution failed. */\n node: string;\n\n /** Error thrown by the failed node. */\n error: Error;\n\n constructor(node: string, error: Error) {\n this.node = node;\n this.error = error;\n }\n\n static get unminifiable_name() {\n return \"NodeError\";\n }\n}\n\n/**\n * Type guard that checks whether a value is a {@link NodeError}.\n */\nexport function isNodeError(e?: unknown): e is NodeError {\n return (\n e != null &&\n typeof e === \"object\" &&\n e.constructor != null &&\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (e.constructor as any).unminifiable_name === NodeError.unminifiable_name\n );\n}\n\nexport class ParentCommand extends GraphBubbleUp {\n command: Command;\n\n constructor(command: Command) {\n super();\n this.name = \"ParentCommand\";\n this.command = command;\n }\n\n static get unminifiable_name() {\n return \"ParentCommand\";\n }\n}\n\nexport function isParentCommand(e?: unknown): e is ParentCommand {\n return (\n e !== undefined &&\n (e as ParentCommand).name === ParentCommand.unminifiable_name\n );\n}\n\nexport function isGraphBubbleUp(e?: unknown): e is GraphBubbleUp {\n return e !== undefined && (e as GraphBubbleUp).is_bubble_up === true;\n}\n\nexport function isGraphInterrupt(e?: unknown): e is GraphInterrupt {\n return (\n e !== undefined &&\n [\n GraphInterrupt.unminifiable_name,\n NodeInterrupt.unminifiable_name,\n ].includes((e as Error).name)\n );\n}\n\n/**\n * Raised when a node invocation exceeds one of its configured timeouts.\n *\n * Does **not** extend {@link GraphBubbleUp} (so it flows through the normal node\n * error path) and is intentionally treated as retryable by the default retry\n * policy — its message/name do not match the default `retryOn` blocklist, so a\n * configured {@link RetryPolicy} will retry it (see langchain-ai/langgraph#7659).\n *\n * Both {@link NodeTimeoutError.runTimeout} and {@link NodeTimeoutError.idleTimeout}\n * reflect the configured policy at the time of the failure (each `undefined` if\n * not configured). {@link NodeTimeoutError.kind} and {@link NodeTimeoutError.timeout}\n * identify which one fired.\n *\n * @category Errors\n */\nexport class NodeTimeoutError extends BaseLangGraphError {\n /** Name of the node/task that timed out. */\n node: string;\n\n /** Which timeout fired: a hard `\"run\"` cap or a progress-resetting `\"idle\"` cap. */\n kind: \"run\" | \"idle\";\n\n /** The value (ms) of the timeout that fired (`runTimeout` or `idleTimeout`). */\n timeout: number;\n\n /** Elapsed time (ms) since the attempt started, at the moment the timeout fired. */\n elapsed: number;\n\n /** Configured run timeout (ms), if any. */\n runTimeout?: number;\n\n /** Configured idle timeout (ms), if any. */\n idleTimeout?: number;\n\n constructor(\n fields: {\n node: string;\n elapsed: number;\n kind: \"run\" | \"idle\";\n runTimeout?: number;\n idleTimeout?: number;\n },\n errorFields?: BaseLangGraphErrorFields\n ) {\n const { node, elapsed, kind, runTimeout, idleTimeout } = fields;\n let message: string;\n let timeout: number;\n if (kind === \"idle\") {\n if (idleTimeout === undefined) {\n throw new Error(\"idleTimeout is required when kind='idle'\");\n }\n timeout = idleTimeout;\n message =\n `Node \"${node}\" exceeded its idle timeout of ${idleTimeout}ms ` +\n `without making progress (elapsed: ${elapsed}ms).`;\n } else {\n if (runTimeout === undefined) {\n throw new Error(\"runTimeout is required when kind='run'\");\n }\n timeout = runTimeout;\n message =\n `Node \"${node}\" exceeded its run timeout of ${runTimeout}ms ` +\n `(elapsed: ${elapsed}ms).`;\n }\n super(message, errorFields);\n this.name = \"NodeTimeoutError\";\n this.node = node;\n this.kind = kind;\n this.timeout = timeout;\n this.elapsed = elapsed;\n this.runTimeout = runTimeout;\n this.idleTimeout = idleTimeout;\n }\n\n static get unminifiable_name() {\n return \"NodeTimeoutError\";\n }\n}\n\nexport function isNodeTimeoutError(e?: unknown): e is NodeTimeoutError {\n return (\n e !== undefined &&\n (e as NodeTimeoutError).name === NodeTimeoutError.unminifiable_name\n );\n}\n\nexport class EmptyInputError extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"EmptyInputError\";\n }\n\n static get unminifiable_name() {\n return \"EmptyInputError\";\n }\n}\n\nexport class EmptyChannelError extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n // Skip expensive stack trace capture — used for control flow on channel reads.\n const prevLimit = Error.stackTraceLimit;\n Error.stackTraceLimit = 0;\n super(message, fields);\n Error.stackTraceLimit = prevLimit;\n this.name = \"EmptyChannelError\";\n }\n\n static get unminifiable_name() {\n return \"EmptyChannelError\";\n }\n}\n\nexport class InvalidUpdateError extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"InvalidUpdateError\";\n }\n\n static get unminifiable_name() {\n return \"InvalidUpdateError\";\n }\n}\n\n/**\n * @deprecated This exception type is no longer thrown.\n */\nexport class MultipleSubgraphsError extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"MultipleSubgraphError\";\n }\n\n static get unminifiable_name() {\n return \"MultipleSubgraphError\";\n }\n}\n\nexport class UnreachableNodeError extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"UnreachableNodeError\";\n }\n\n static get unminifiable_name() {\n return \"UnreachableNodeError\";\n }\n}\n\n/**\n * Exception raised when an error occurs in the remote graph.\n */\nexport class RemoteException extends BaseLangGraphError {\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"RemoteException\";\n }\n\n static get unminifiable_name() {\n return \"RemoteException\";\n }\n}\n\n/**\n * Error thrown when invalid input is provided to a StateGraph.\n *\n * This typically means that the input to the StateGraph constructor or builder\n * did not match the required types. A valid input should be a\n * StateDefinition, an Annotation.Root, or a Zod schema.\n *\n * @example\n * // Example of incorrect usage:\n * try {\n * new StateGraph({ foo: \"bar\" }); // Not a valid input\n * } catch (err) {\n * if (err instanceof StateGraphInputError) {\n * console.error(err.message);\n * }\n * }\n */\nexport class StateGraphInputError extends BaseLangGraphError {\n /**\n * Create a new StateGraphInputError.\n * @param message - Optional custom error message.\n * @param fields - Optional additional error fields.\n */\n constructor(message?: string, fields?: BaseLangGraphErrorFields) {\n super(message, fields);\n this.name = \"StateGraphInputError\";\n this.message =\n \"Invalid StateGraph input. Make sure to pass a valid StateDefinition, Annotation.Root, or Zod schema.\";\n }\n\n /**\n * The unminifiable (static, human-readable) error name for this error class.\n */\n static get unminifiable_name() {\n return \"StateGraphInputError\";\n }\n}\n\n/**\n * Used for subgraph detection.\n */\nexport const getSubgraphsSeenSet = () => {\n if (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (globalThis as any)[Symbol.for(\"LG_CHECKPOINT_SEEN_NS_SET\")] === undefined\n ) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (globalThis as any)[Symbol.for(\"LG_CHECKPOINT_SEEN_NS_SET\")] = new Set();\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (globalThis as any)[Symbol.for(\"LG_CHECKPOINT_SEEN_NS_SET\")];\n};\n"],"mappings":";;AAgBA,IAAa,qBAAb,cAAwC,MAAM;CAC5C;CAEA,YAAY,SAAkB,QAAmC;EAC/D,IAAI,eAAe,WAAW;AAC9B,MAAI,QAAQ,cACV,gBAAe,GAAG,aAAa,+EAA+E,OAAO,cAAc;AAErI,QAAM,aAAa;AACnB,OAAK,gBAAgB,QAAQ;;;AAIjC,IAAa,gBAAb,cAAmC,mBAAmB;CACpD,IAAI,eAAe;AACjB,SAAO;;;AAIX,IAAa,sBAAb,cAAyC,mBAAmB;CAC1D,YAAY,SAAkB,QAAmC;AAC/D,QAAM,SAAS,OAAO;AACtB,OAAK,OAAO;;CAGd,WAAW,oBAAoB;AAC7B,SAAO;;;AAIX,IAAa,kBAAb,cAAqC,mBAAmB;CACtD,YAAY,SAAkB,QAAmC;AAC/D,QAAM,SAAS,OAAO;AACtB,OAAK,OAAO;;CAGd,WAAW,oBAAoB;AAC7B,SAAO;;;;;;;;;;AAWX,IAAa,eAAb,cAAkC,cAAc;CAC9C;CAEA,YAAY,SAAiB,YAAY,QAAmC;AAC1E,QAAM,kBAAkB,UAAU,OAAO;AACzC,OAAK,OAAO;AACZ,OAAK,SAAS;;CAGhB,WAAW,oBAAoB;AAC7B,SAAO;;;AAIX,SAAgB,eAAe,GAAgC;AAC7D,QACE,MAAM,KAAA,KAAc,EAAY,SAAS,aAAa;;AAI1D,IAAa,iBAAb,cAAoC,cAAc;CAChD;CAEA,YAAY,YAA0B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