@jungjaehoon/mama-server 1.7.2 → 1.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,326 +0,0 @@
1
- /**
2
- * MAMA Error Classes - Typed Error Handling
3
- *
4
- * Story 8.3: Typed Error Classes
5
- * Provides consistent error handling across MCP tools and core modules
6
- *
7
- * Error codes follow MCP standard response format:
8
- * {error: {code: 'ERROR_CODE', message: '...', details: {}}}
9
- *
10
- * @module errors
11
- * @version 1.0
12
- * @date 2025-11-25
13
- */
14
-
15
- /**
16
- * Base error class for all MAMA errors
17
- *
18
- * @class MAMAError
19
- * @extends Error
20
- */
21
- class MAMAError extends Error {
22
- /**
23
- * @param {string} message - Error message
24
- * @param {string} code - Error code (e.g., 'DECISION_NOT_FOUND')
25
- * @param {Object} details - Additional error details
26
- */
27
- constructor(message, code = 'MAMA_ERROR', details = {}) {
28
- super(message);
29
- this.name = 'MAMAError';
30
- this.code = code;
31
- this.details = details;
32
- this.timestamp = new Date().toISOString();
33
-
34
- // Capture stack trace
35
- if (Error.captureStackTrace) {
36
- Error.captureStackTrace(this, this.constructor);
37
- }
38
- }
39
-
40
- /**
41
- * Convert to MCP-compatible error response format
42
- *
43
- * @returns {Object} {error: {code, message, details}}
44
- */
45
- toResponse() {
46
- return {
47
- error: {
48
- code: this.code,
49
- message: this.message,
50
- details: this.details,
51
- },
52
- };
53
- }
54
-
55
- /**
56
- * Convert to JSON for logging
57
- *
58
- * @returns {Object} JSON representation
59
- */
60
- toJSON() {
61
- return {
62
- name: this.name,
63
- code: this.code,
64
- message: this.message,
65
- details: this.details,
66
- timestamp: this.timestamp,
67
- stack: this.stack,
68
- };
69
- }
70
- }
71
-
72
- /**
73
- * Error thrown when a decision is not found
74
- *
75
- * @class NotFoundError
76
- * @extends MAMAError
77
- */
78
- class NotFoundError extends MAMAError {
79
- /**
80
- * @param {string} resourceType - Type of resource (e.g., 'decision', 'checkpoint')
81
- * @param {string} identifier - Resource identifier
82
- * @param {Object} details - Additional details
83
- */
84
- constructor(resourceType, identifier, details = {}) {
85
- super(`${resourceType} not found: ${identifier}`, `${resourceType.toUpperCase()}_NOT_FOUND`, {
86
- resourceType,
87
- identifier,
88
- ...details,
89
- });
90
- this.name = 'NotFoundError';
91
- }
92
- }
93
-
94
- /**
95
- * Error thrown when input validation fails
96
- *
97
- * @class ValidationError
98
- * @extends MAMAError
99
- */
100
- class ValidationError extends MAMAError {
101
- /**
102
- * @param {string} field - Field that failed validation
103
- * @param {string} message - Validation error message
104
- * @param {*} received - Received value
105
- * @param {Object} details - Additional details
106
- */
107
- constructor(field, message, received = undefined, details = {}) {
108
- super(`Validation failed for '${field}': ${message}`, 'INVALID_INPUT', {
109
- field,
110
- received: received !== undefined ? String(received).substring(0, 100) : undefined,
111
- ...details,
112
- });
113
- this.name = 'ValidationError';
114
- this.field = field;
115
- }
116
- }
117
-
118
- /**
119
- * Error thrown when database operations fail
120
- *
121
- * @class DatabaseError
122
- * @extends MAMAError
123
- */
124
- class DatabaseError extends MAMAError {
125
- /**
126
- * @param {string} operation - Database operation (e.g., 'insert', 'query', 'update')
127
- * @param {string} message - Error message
128
- * @param {Object} details - Additional details
129
- */
130
- constructor(operation, message, details = {}) {
131
- super(`Database ${operation} failed: ${message}`, 'DATABASE_ERROR', {
132
- operation,
133
- ...details,
134
- });
135
- this.name = 'DatabaseError';
136
- this.operation = operation;
137
- }
138
- }
139
-
140
- /**
141
- * Error thrown when embedding generation fails
142
- *
143
- * @class EmbeddingError
144
- * @extends MAMAError
145
- */
146
- class EmbeddingError extends MAMAError {
147
- /**
148
- * @param {string} message - Error message
149
- * @param {Object} details - Additional details (model, input length, etc.)
150
- */
151
- constructor(message, details = {}) {
152
- super(`Embedding generation failed: ${message}`, 'EMBEDDING_ERROR', details);
153
- this.name = 'EmbeddingError';
154
- }
155
- }
156
-
157
- /**
158
- * Error thrown when configuration is invalid
159
- *
160
- * @class ConfigurationError
161
- * @extends MAMAError
162
- */
163
- class ConfigurationError extends MAMAError {
164
- /**
165
- * @param {string} configKey - Configuration key
166
- * @param {string} message - Error message
167
- * @param {Object} details - Additional details
168
- */
169
- constructor(configKey, message, details = {}) {
170
- super(`Configuration error for '${configKey}': ${message}`, 'CONFIG_ERROR', {
171
- configKey,
172
- ...details,
173
- });
174
- this.name = 'ConfigurationError';
175
- this.configKey = configKey;
176
- }
177
- }
178
-
179
- /**
180
- * Error thrown when a link operation fails
181
- *
182
- * @class LinkError
183
- * @extends MAMAError
184
- */
185
- class LinkError extends MAMAError {
186
- /**
187
- * @param {string} operation - Link operation (e.g., 'propose', 'approve', 'reject')
188
- * @param {string} message - Error message
189
- * @param {Object} details - Additional details (from_id, to_id, etc.)
190
- */
191
- constructor(operation, message, details = {}) {
192
- super(`Link ${operation} failed: ${message}`, 'LINK_ERROR', {
193
- operation,
194
- ...details,
195
- });
196
- this.name = 'LinkError';
197
- this.operation = operation;
198
- }
199
- }
200
-
201
- /**
202
- * Error thrown when rate limit is exceeded
203
- *
204
- * @class RateLimitError
205
- * @extends MAMAError
206
- */
207
- class RateLimitError extends MAMAError {
208
- /**
209
- * @param {string} operation - Operation that was rate limited
210
- * @param {number} retryAfterMs - Time to wait before retry (ms)
211
- * @param {Object} details - Additional details
212
- */
213
- constructor(operation, retryAfterMs, details = {}) {
214
- super(`Rate limit exceeded for ${operation}. Retry after ${retryAfterMs}ms`, 'RATE_LIMITED', {
215
- operation,
216
- retryAfterMs,
217
- ...details,
218
- });
219
- this.name = 'RateLimitError';
220
- this.retryAfterMs = retryAfterMs;
221
- }
222
- }
223
-
224
- /**
225
- * Error thrown when operation times out
226
- *
227
- * @class TimeoutError
228
- * @extends MAMAError
229
- */
230
- class TimeoutError extends MAMAError {
231
- /**
232
- * @param {string} operation - Operation that timed out
233
- * @param {number} timeoutMs - Timeout duration (ms)
234
- * @param {Object} details - Additional details
235
- */
236
- constructor(operation, timeoutMs, details = {}) {
237
- super(`Operation '${operation}' timed out after ${timeoutMs}ms`, 'TIMEOUT', {
238
- operation,
239
- timeoutMs,
240
- ...details,
241
- });
242
- this.name = 'TimeoutError';
243
- this.timeoutMs = timeoutMs;
244
- }
245
- }
246
-
247
- /**
248
- * Error codes enum for reference
249
- */
250
- const ErrorCodes = {
251
- // Resource errors
252
- DECISION_NOT_FOUND: 'DECISION_NOT_FOUND',
253
- CHECKPOINT_NOT_FOUND: 'CHECKPOINT_NOT_FOUND',
254
- LINK_NOT_FOUND: 'LINK_NOT_FOUND',
255
-
256
- // Validation errors
257
- INVALID_INPUT: 'INVALID_INPUT',
258
- MISSING_REQUIRED_FIELD: 'MISSING_REQUIRED_FIELD',
259
- INVALID_FORMAT: 'INVALID_FORMAT',
260
-
261
- // Database errors
262
- DATABASE_ERROR: 'DATABASE_ERROR',
263
- CONNECTION_FAILED: 'CONNECTION_FAILED',
264
- QUERY_FAILED: 'QUERY_FAILED',
265
-
266
- // Processing errors
267
- EMBEDDING_ERROR: 'EMBEDDING_ERROR',
268
- CONFIG_ERROR: 'CONFIG_ERROR',
269
- LINK_ERROR: 'LINK_ERROR',
270
-
271
- // Operational errors
272
- RATE_LIMITED: 'RATE_LIMITED',
273
- TIMEOUT: 'TIMEOUT',
274
- INTERNAL_ERROR: 'INTERNAL_ERROR',
275
- };
276
-
277
- /**
278
- * Helper function to wrap unknown errors
279
- *
280
- * @param {Error|unknown} error - Error to wrap
281
- * @param {string} context - Context for the error
282
- * @returns {MAMAError} Wrapped MAMA error
283
- */
284
- function wrapError(error, context = 'Unknown operation') {
285
- if (error instanceof MAMAError) {
286
- return error;
287
- }
288
-
289
- const message = error instanceof Error ? error.message : String(error);
290
- const stack = error instanceof Error ? error.stack : undefined;
291
-
292
- return new MAMAError(`${context}: ${message}`, 'INTERNAL_ERROR', {
293
- originalError: message,
294
- originalStack: stack,
295
- });
296
- }
297
-
298
- /**
299
- * Helper function to check if an error is a MAMA error
300
- *
301
- * @param {unknown} error - Error to check
302
- * @returns {boolean} True if MAMA error
303
- */
304
- function isMAMAError(error) {
305
- return error instanceof MAMAError;
306
- }
307
-
308
- module.exports = {
309
- // Base class
310
- MAMAError,
311
-
312
- // Specific error types
313
- NotFoundError,
314
- ValidationError,
315
- DatabaseError,
316
- EmbeddingError,
317
- ConfigurationError,
318
- LinkError,
319
- RateLimitError,
320
- TimeoutError,
321
-
322
- // Utilities
323
- ErrorCodes,
324
- wrapError,
325
- isMAMAError,
326
- };