File size: 7,866 Bytes
2d661b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
 * Domain-specific error classes for the Engram system.
 *
 * These errors represent specific failure scenarios across the system.
 *
 * @module @engram/common/errors/domain
 */

import { EngramError } from "./base";

/**
 * Error codes for domain errors.
 */
export const ErrorCodes = {
	// Graph operations
	GRAPH_QUERY_FAILED: "GRAPH_QUERY_FAILED",
	GRAPH_CONNECTION_FAILED: "GRAPH_CONNECTION_FAILED",
	GRAPH_TRANSACTION_FAILED: "GRAPH_TRANSACTION_FAILED",

	// Parsing
	PARSE_JSON_FAILED: "PARSE_JSON_FAILED",
	PARSE_CYPHER_FAILED: "PARSE_CYPHER_FAILED",
	PARSE_EVENT_FAILED: "PARSE_EVENT_FAILED",

	// Validation
	VALIDATION_FAILED: "VALIDATION_FAILED",
	VALIDATION_SCHEMA_FAILED: "VALIDATION_SCHEMA_FAILED",
	VALIDATION_CONSTRAINT_FAILED: "VALIDATION_CONSTRAINT_FAILED",

	// Context assembly
	CONTEXT_ASSEMBLY_FAILED: "CONTEXT_ASSEMBLY_FAILED",
	CONTEXT_TIMEOUT: "CONTEXT_TIMEOUT",
	CONTEXT_LIMIT_EXCEEDED: "CONTEXT_LIMIT_EXCEEDED",

	// Rehydration
	REHYDRATION_FAILED: "REHYDRATION_FAILED",
	REHYDRATION_NOT_FOUND: "REHYDRATION_NOT_FOUND",
	REHYDRATION_CORRUPTED: "REHYDRATION_CORRUPTED",

	// Storage
	STORAGE_READ_FAILED: "STORAGE_READ_FAILED",
	STORAGE_WRITE_FAILED: "STORAGE_WRITE_FAILED",
	STORAGE_NOT_FOUND: "STORAGE_NOT_FOUND",
	STORAGE_INVALID_PATH: "STORAGE_INVALID_PATH",

	// Search
	SEARCH_QUERY_FAILED: "SEARCH_QUERY_FAILED",
	SEARCH_INDEX_FAILED: "SEARCH_INDEX_FAILED",
	SEARCH_EMBEDDING_FAILED: "SEARCH_EMBEDDING_FAILED",
} as const;

export type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];

/**
 * Error for FalkorDB graph operations.
 *
 * Thrown when graph queries, connections, or transactions fail.
 *
 * @example
 * ```ts
 * throw new GraphOperationError(
 *   "Failed to execute Cypher query",
 *   "MATCH (n) RETURN n LIMIT 10",
 *   originalError
 * );
 * ```
 */
export class GraphOperationError extends EngramError {
	/**
	 * The Cypher query that failed (if applicable).
	 */
	public readonly query?: string;

	/**
	 * Query parameters (if applicable).
	 */
	public readonly params?: Record<string, unknown>;

	constructor(message: string, query?: string, cause?: Error, params?: Record<string, unknown>) {
		super(message, ErrorCodes.GRAPH_QUERY_FAILED, cause);
		this.name = "GraphOperationError";
		this.query = query;
		this.params = params;
	}

	override toJSON(): Record<string, unknown> {
		return {
			...super.toJSON(),
			query: this.query,
			params: this.params,
		};
	}
}

/**
 * Error for parsing failures.
 *
 * Thrown when JSON parsing, event parsing, or other deserialization fails.
 *
 * @example
 * ```ts
 * throw new ParseError("Invalid JSON in event payload", rawPayload, originalError);
 * ```
 */
export class ParseError extends EngramError {
	/**
	 * The raw input that failed to parse.
	 */
	public readonly input?: string;

	/**
	 * Expected format or type.
	 */
	public readonly expected?: string;

	constructor(message: string, input?: string, cause?: Error, expected?: string) {
		super(message, ErrorCodes.PARSE_JSON_FAILED, cause);
		this.name = "ParseError";
		this.input = input ? input.slice(0, 500) : undefined; // Truncate for logging
		this.expected = expected;
	}

	override toJSON(): Record<string, unknown> {
		return {
			...super.toJSON(),
			input: this.input,
			expected: this.expected,
		};
	}
}

/**
 * Error for validation failures.
 *
 * Thrown when input validation, schema validation, or constraint checks fail.
 *
 * @example
 * ```ts
 * throw new ValidationError("Session ID is required", "sessionId");
 * ```
 */
export class ValidationError extends EngramError {
	/**
	 * The field or property that failed validation.
	 */
	public readonly field?: string;

	/**
	 * The invalid value that was provided.
	 */
	public readonly value?: unknown;

	/**
	 * Constraint that was violated.
	 */
	public readonly constraint?: string;

	constructor(
		message: string,
		field?: string,
		cause?: Error,
		options?: { value?: unknown; constraint?: string },
	) {
		super(message, ErrorCodes.VALIDATION_FAILED, cause);
		this.name = "ValidationError";
		this.field = field;
		this.value = options?.value;
		this.constraint = options?.constraint;
	}

	override toJSON(): Record<string, unknown> {
		return {
			...super.toJSON(),
			field: this.field,
			value: this.value,
			constraint: this.constraint,
		};
	}
}

/**
 * Error for context assembly failures.
 *
 * Thrown when building context for agent interactions fails.
 *
 * @example
 * ```ts
 * throw new ContextAssemblyError(
 *   "Failed to assemble context within timeout",
 *   sessionId,
 *   timeoutError
 * );
 * ```
 */
export class ContextAssemblyError extends EngramError {
	/**
	 * Session ID for which context assembly failed.
	 */
	public readonly sessionId?: string;

	/**
	 * Partial context that was assembled before failure.
	 */
	public readonly partialContext?: unknown;

	constructor(message: string, sessionId?: string, cause?: Error, partialContext?: unknown) {
		super(message, ErrorCodes.CONTEXT_ASSEMBLY_FAILED, cause);
		this.name = "ContextAssemblyError";
		this.sessionId = sessionId;
		this.partialContext = partialContext;
	}

	override toJSON(): Record<string, unknown> {
		return {
			...super.toJSON(),
			sessionId: this.sessionId,
			hasPartialContext: this.partialContext !== undefined,
		};
	}
}

/**
 * Error for state rehydration failures.
 *
 * Thrown when reconstructing state from persisted data fails.
 *
 * @example
 * ```ts
 * throw new RehydrationError("Session state corrupted", sessionId, corruptionError);
 * ```
 */
export class RehydrationError extends EngramError {
	/**
	 * Entity ID for which rehydration failed.
	 */
	public readonly entityId?: string;

	/**
	 * Type of entity being rehydrated.
	 */
	public readonly entityType?: string;

	constructor(message: string, entityId?: string, cause?: Error, entityType?: string) {
		super(message, ErrorCodes.REHYDRATION_FAILED, cause);
		this.name = "RehydrationError";
		this.entityId = entityId;
		this.entityType = entityType;
	}

	override toJSON(): Record<string, unknown> {
		return {
			...super.toJSON(),
			entityId: this.entityId,
			entityType: this.entityType,
		};
	}
}

/**
 * Error for storage operations.
 *
 * Thrown when blob storage, file system, or database operations fail.
 *
 * @example
 * ```ts
 * throw new StorageError("Failed to read blob", uri, readError);
 * ```
 */
export class StorageError extends EngramError {
	/**
	 * URI or path of the storage resource.
	 */
	public readonly uri?: string;

	/**
	 * Type of storage operation that failed.
	 */
	public readonly operation?: "read" | "write" | "delete" | "list";

	constructor(
		message: string,
		code: ErrorCode,
		uri?: string,
		cause?: Error,
		operation?: "read" | "write" | "delete" | "list",
	) {
		super(message, code, cause);
		this.name = "StorageError";
		this.uri = uri;
		this.operation = operation;
	}

	override toJSON(): Record<string, unknown> {
		return {
			...super.toJSON(),
			uri: this.uri,
			operation: this.operation,
		};
	}
}

/**
 * Error for search operations.
 *
 * Thrown when search queries, indexing, or embedding generation fails.
 *
 * @example
 * ```ts
 * throw new SearchError("Embedding generation failed", query, embeddingError);
 * ```
 */
export class SearchError extends EngramError {
	/**
	 * The search query that failed.
	 */
	public readonly query?: string;

	/**
	 * Search operation that failed.
	 */
	public readonly operation?: "query" | "index" | "embed" | "rerank";

	constructor(
		message: string,
		code: ErrorCode,
		query?: string,
		cause?: Error,
		operation?: "query" | "index" | "embed" | "rerank",
	) {
		super(message, code, cause);
		this.name = "SearchError";
		this.query = query;
		this.operation = operation;
	}

	override toJSON(): Record<string, unknown> {
		return {
			...super.toJSON(),
			query: this.query,
			operation: this.operation,
		};
	}
}