-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handler.ts
More file actions
335 lines (288 loc) · 8.6 KB
/
error-handler.ts
File metadata and controls
335 lines (288 loc) · 8.6 KB
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
/**
* @module utils/error-handler
* @description Centralized error handling for commands with structured logging.
*/
import chalk from 'chalk';
import type { Ora } from 'ora';
import {
UMSValidationError,
UMSModuleLoadError,
PersonaLoadError,
BuildError,
ConflictError,
isUMSError,
type UMSError,
type ErrorLocation,
} from 'ums-sdk';
/**
* Error handler with M0.5 standardized formatting support
*/
export interface ErrorHandlerOptions {
command: string;
context?: string;
suggestion?: string;
filePath?: string;
keyPath?: string;
verbose?: boolean;
timestamp?: boolean;
}
/**
* Format error location for display
*/
function formatLocation(location: ErrorLocation): string {
const parts: string[] = [];
if (location.filePath) {
parts.push(location.filePath);
}
if (location.line !== undefined) {
if (location.column !== undefined) {
parts.push(`line ${location.line}, column ${location.column}`);
} else {
parts.push(`line ${location.line}`);
}
}
return parts.join(':');
}
/**
* Format spec section reference for display
*/
function formatSpecSection(specSection: string): string {
return chalk.cyan(` Specification: ${specSection}`);
}
/**
* Handle ConflictError with specific formatting
*/
function handleConflictError(
error: ConflictError,
command: string,
context?: string
): void {
const contextPart = context ?? 'module resolution';
const formattedMessage = `[ERROR] ${command}: ${contextPart} - ${error.message}`;
console.error(chalk.red(formattedMessage));
console.error(chalk.yellow(' Conflict Details:'));
console.error(chalk.yellow(` Module ID: ${error.moduleId}`));
console.error(
chalk.yellow(` Conflicting versions: ${error.conflictCount}`)
);
const suggestions = [
'Use --conflict-strategy=warn to resolve with warnings',
'Use --conflict-strategy=replace to use the latest version',
'Remove duplicate modules from different sources',
'Check your module configuration files',
];
console.error(chalk.blue(' Suggestions:'));
suggestions.forEach(suggestion => {
console.error(chalk.blue(` • ${suggestion}`));
});
}
/**
* Handle validation errors with file and section info
*/
function handleValidationError(
error: UMSValidationError,
_command: string,
_context?: string
): void {
// Error header
console.error(chalk.red(`❌ Error: ${error.message}`));
console.error();
// Location information
if (error.location) {
console.error(
chalk.yellow(` Location: ${formatLocation(error.location)}`)
);
} else if (error.path) {
console.error(chalk.yellow(` File: ${error.path}`));
}
// Field path (if available)
if (error.section) {
console.error(chalk.yellow(` Field: ${error.section}`));
}
// Spec reference
if (error.specSection) {
console.error(formatSpecSection(error.specSection));
}
console.error();
// Suggestions
const suggestions = [
'Check YAML/TypeScript syntax and structure',
'Verify all required fields are present',
'Review UMS specification for correct format',
];
console.error(chalk.blue(' Suggestions:'));
suggestions.forEach(suggestion => {
console.error(chalk.blue(` • ${suggestion}`));
});
}
/**
* Handle module/persona load errors with file path info
*/
function handleLoadError(
error: UMSModuleLoadError | PersonaLoadError,
_command: string,
_context?: string
): void {
const isModule = error instanceof UMSModuleLoadError;
// Error header
console.error(chalk.red(`❌ Error: ${error.message}`));
console.error();
// Location information
if (error.location) {
console.error(
chalk.yellow(` Location: ${formatLocation(error.location)}`)
);
} else if (error.filePath) {
console.error(chalk.yellow(` File: ${error.filePath}`));
}
// Spec reference
if (error.specSection) {
console.error(formatSpecSection(error.specSection));
}
console.error();
// Suggestions
const suggestions = isModule
? [
'Check file exists and is readable',
'Verify file path is correct',
'Ensure file contains valid YAML or TypeScript content',
'Check module ID matches export name for TypeScript modules',
]
: [
'Check persona file exists and is readable',
'Verify persona YAML/TypeScript structure',
'Ensure all referenced modules exist',
'Check export format for TypeScript personas',
];
console.error(chalk.blue(' Suggestions:'));
suggestions.forEach(suggestion => {
console.error(chalk.blue(` • ${suggestion}`));
});
}
/**
* Enhanced error handling for UMS-specific error types
*/
function handleUMSError(error: UMSError, options: ErrorHandlerOptions): void {
const { command, context, verbose, timestamp } = options;
// Handle specific UMS error types
if (error instanceof ConflictError) {
handleConflictError(error, command, context);
} else if (error instanceof UMSValidationError) {
handleValidationError(error, command, context);
} else if (
error instanceof UMSModuleLoadError ||
error instanceof PersonaLoadError
) {
handleLoadError(error, command, context);
} else if (error instanceof BuildError) {
const contextPart = context ?? 'build process';
const formattedMessage = `[ERROR] ${command}: ${contextPart} - ${error.message}`;
console.error(chalk.red(formattedMessage));
const suggestions = [
'Check persona and module files are valid',
'Verify all dependencies are available',
'Review error details above',
];
console.error(chalk.blue(' Suggestions:'));
suggestions.forEach(suggestion => {
console.error(chalk.blue(` • ${suggestion}`));
});
} else {
// Generic UMS error
// Error header
console.error(chalk.red(`❌ Error: ${error.message}`));
console.error();
// Location information
if (error.location) {
console.error(
chalk.yellow(` Location: ${formatLocation(error.location)}`)
);
}
// Context
if (error.context) {
console.error(chalk.yellow(` Context: ${error.context}`));
}
// Spec reference
if (error.specSection) {
console.error(formatSpecSection(error.specSection));
}
console.error();
const suggestions = [
'Review error details and try again',
'Check UMS specification for guidance',
];
console.error(chalk.blue(' Suggestions:'));
suggestions.forEach(suggestion => {
console.error(chalk.blue(` • ${suggestion}`));
});
}
// Add verbose output if requested
if (verbose && timestamp) {
console.error();
const ts = new Date().toISOString();
console.error(chalk.gray(`[${ts}] [ERROR] Error code: ${error.code}`));
if (error.stack) {
console.error(chalk.gray(`[${ts}] [ERROR] Stack trace:`));
console.error(chalk.gray(error.stack));
}
}
}
/**
* Handles errors from command handlers using M0.5 standard format.
* Format: [ERROR] <command>: <context> - <specific issue> (<suggestion>)
* @param error - The error object.
* @param options - Error handling options following M0.5 standards.
*/
export function handleError(
error: unknown,
options: ErrorHandlerOptions
): void {
const {
command,
context,
suggestion,
filePath,
keyPath,
verbose,
timestamp,
} = options;
// Handle UMS-specific errors with enhanced formatting
if (isUMSError(error)) {
handleUMSError(error, options);
return;
}
// Handle generic errors
const errorMessage = error instanceof Error ? error.message : String(error);
const contextPart = context ?? 'operation failed';
const suggestionPart = suggestion ?? 'check the error details and try again';
let formattedMessage = `[ERROR] ${command}: ${contextPart} - ${errorMessage} (${suggestionPart})`;
if (filePath) {
formattedMessage += `\n File: ${filePath}`;
}
if (keyPath) {
formattedMessage += `\n Key path: ${keyPath}`;
}
if (verbose && timestamp) {
const ts = new Date().toISOString();
console.error(chalk.gray(`[${ts}]`), chalk.red(formattedMessage));
if (error instanceof Error && error.stack) {
console.error(chalk.gray(`[${ts}] [ERROR] Stack trace:`));
console.error(chalk.gray(error.stack));
}
} else {
console.error(chalk.red(formattedMessage));
}
}
/**
* Legacy method for backwards compatibility
*/
export function handleErrorLegacy(error: unknown, spinner?: Ora): void {
if (spinner) {
spinner.fail(chalk.red('Operation failed.'));
} else {
console.error(chalk.red('Operation failed.'));
}
if (error instanceof Error) {
console.error(chalk.red(error.message));
}
}