-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli-error-handler.ts
More file actions
309 lines (273 loc) · 9.79 KB
/
cli-error-handler.ts
File metadata and controls
309 lines (273 loc) · 9.79 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
import { AxiosError } from 'axios';
import { ClassifiedError, ErrorContext } from '../interfaces';
import { formatError } from '../helpers';
import { ERROR_TYPES } from '../constants/errorTypes';
import { redactObject } from '../helpers';
/**
* Handles errors in a CLI application by classifying, normalizing, and extracting
* relevant information for debugging and logging purposes.
*
* This class provides methods to:
* - Normalize unknown error types into standard `Error` objects.
* - Classify errors into predefined categories such as API errors, network errors,
* server errors, and more.
* - Extract detailed error payloads for logging, including HTTP request and response
* details when applicable.
* - Identify sensitive information in error messages to prevent accidental exposure.
* - Generate debug payloads for enhanced troubleshooting when debugging is enabled.
*
* @remarks
* This class is designed to handle a wide range of error types, including generic
* JavaScript errors, API errors, and custom error objects. It also supports
* optional debugging and context metadata for enhanced error reporting.
*
* @example
* ```typescript
* const errorHandler = new CLIErrorHandler();
*
* try {
* // Some operation that may throw an error
* } catch (error) {
* const classifiedError = errorHandler.classifyError(error, {
* operation: 'fetchData',
* component: 'DataService',
* });
* console.error(classifiedError);
* }
* ```
*
* @public
*/
export default class CLIErrorHandler {
constructor() {}
/**
* Classifies an error into a structured format for better handling and debugging.
*
* @param error - The error object to classify. Can be of any type.
* @param context - Optional additional context about the error.
* @param errMessage - Optional custom error message to override the default error message.
*
* @returns A `ClassifiedError` object containing essential error details in a clear,
* concise format optimized for debugging.
*/
classifyError(error: unknown, context?: ErrorContext, errMessage?: string): ClassifiedError {
try {
const normalized = this.normalizeToError(error);
const type = this.determineErrorType(normalized);
const hidden = this.containsSensitiveInfo(normalized);
const result: ClassifiedError = {
type,
message: errMessage || this.extractClearMessage(normalized),
error: this.extractErrorPayload(normalized),
meta: this.extractMeta(context, type),
hidden,
};
return result;
} catch (e) {
return {
type: ERROR_TYPES.NORMALIZATION,
message: 'Failed to process error',
error: {
originalError: String(e),
errorType: typeof error,
},
meta: this.extractMeta(context, ERROR_TYPES.NORMALIZATION),
hidden: false,
};
}
}
/**
* Extracts a clear, concise error message from various error types.
*/
private extractClearMessage(error: Error & Record<string, any>): string {
// Use existing formatError function for other cases
try {
const formattedMessage = formatError(error);
return formattedMessage || 'An error occurred. Please try again.';
} catch {
// Fallback to basic error message extraction if formatError fails
if (typeof error?.response?.data?.errorMessage === 'string') {
return error.response.data.errorMessage;
}
if (typeof error?.errorMessage === 'string') {
return error.errorMessage;
}
}
}
/**
* Normalizes various error types into a standard Error object.
*
* @param error - The error to normalize
* @returns A normalized Error object
*/
private normalizeToError(error: unknown): Error {
if (!error) return new Error('Unknown error occurred');
if (error instanceof Error) return error;
if (typeof error === 'string') return new Error(error);
if (typeof error === 'object') {
try {
const errorObj = error as Record<string, any>;
const message = errorObj.message || errorObj.error || errorObj.statusText || 'Unknown error';
const normalizedError = new Error(message);
// Only copy essential properties
const essentialProps = [
'code',
'status',
'statusText',
'response',
'request',
'message',
'errorMessage',
'error_message',
'error',
'errors',
];
essentialProps.forEach((prop) => {
if (errorObj[prop] !== undefined) {
(normalizedError as any)[prop] = errorObj[prop];
}
});
return normalizedError;
} catch {
return new Error(JSON.stringify(error));
}
}
return new Error(String(error));
}
/**
* Determines the type of error based on its characteristics.
*/
private determineErrorType(error: Error & Record<string, any>): string {
const { status, code, name, response } = error;
const actualStatus = status || response?.status;
// Network and timeout errors
if (['ECONNREFUSED', 'ENOTFOUND', 'ETIMEDOUT', 'ENETUNREACH'].includes(code)) {
return ERROR_TYPES.NETWORK;
}
// HTTP status-based classification
if (actualStatus) {
if (actualStatus >= 100 && actualStatus < 200) return ERROR_TYPES.INFORMATIONAL;
if (actualStatus >= 300 && actualStatus < 400) return ERROR_TYPES.REDIRECTION;
if (actualStatus >= 400 && actualStatus < 500) return ERROR_TYPES.API_ERROR;
if (actualStatus >= 500) return ERROR_TYPES.SERVER_ERROR;
}
// Specific error types
if (name === 'DatabaseError') return ERROR_TYPES.DATABASE;
if ((error as AxiosError).isAxiosError) return ERROR_TYPES.NETWORK;
return ERROR_TYPES.APPLICATION;
}
/**
* Extracts only essential error payload information for clear debugging.
*/
private extractErrorPayload(error: Error & Record<string, any>): Record<string, any> {
const { name, message, code, status, response, request, config, statusText } = error;
const payload: Record<string, any> = {
name,
message: this.extractClearMessage(error),
};
// Add error identifiers
if (code) payload.code = code;
if (status || response?.status) payload.status = status || response?.status;
// Add detailed field-level errors if available
if (response?.data?.errors && typeof response.data.errors === 'object') {
payload.errors = response.data.errors;
} else if (error?.errors && typeof error.errors === 'object') {
payload.errors = error.errors;
}
// Add error code if available
if (response?.data?.error_code) {
payload.errorCode = response.data.error_code;
} else if (error?.error_code) {
payload.errorCode = error.error_code;
}
// Add request context with sensitive data redaction
if (request || config) {
const requestData = {
method: request?.method || config?.method,
url: request?.url || config?.url,
headers: request?.headers || config?.headers,
data: request?.data || config?.data,
timeout: config?.timeout,
baseURL: config?.baseURL,
params: config?.params,
};
// Use existing redactObject to mask sensitive data
payload.request = redactObject(requestData);
}
// Add response context with sensitive data redaction
if (response) {
const responseData = {
status,
statusText,
headers: response.headers,
data: response.data,
};
// Use existing redactObject to mask sensitive data
payload.response = redactObject(responseData);
}
// Extract user-friendly error message for API errors
if (response?.data?.errorMessage) {
payload.userFriendlyMessage = response.data.errorMessage;
}
// Add stack trace only for non-API errors to avoid clutter
if (
![ERROR_TYPES.API_ERROR, ERROR_TYPES.SERVER_ERROR].includes(
this.determineErrorType(error) as typeof ERROR_TYPES.API_ERROR | typeof ERROR_TYPES.SERVER_ERROR,
)
) {
payload.stack = error.stack?.split('\n').slice(0, 5).join('\n');
}
return payload;
}
/**
* Extracts metadata from the error context and adds additional information.
*
* @param context - The error context to extract metadata from
* @param errorType - Optional error type to include in metadata
* @returns An object containing relevant metadata for debugging
*/
private extractMeta(context?: ErrorContext, errorType?: string): Record<string, string | undefined> {
if (!context) return {};
const baseMeta: Record<string, string | undefined> = {};
if (context.operation) baseMeta.operation = context.operation;
if (context.component) baseMeta.component = context.component;
if (context.userId) baseMeta.userId = context.userId;
if (context.sessionId) baseMeta.sessionId = context.sessionId;
if (context.orgId) baseMeta.orgId = context.orgId;
if (errorType) baseMeta.errorType = errorType;
if (context.email) baseMeta.email = context.email;
return baseMeta;
}
/**
* Checks if error contains sensitive information.
*
* @param error - Error to check
* @returns True if sensitive info is found
*/
private containsSensitiveInfo(error: Error): boolean {
try {
const content = `${error.message} ${error.stack || ''}`.toLowerCase();
const sensitiveTerms = [
'password',
'token',
'secret',
'credentials',
'api_key',
'api-key',
'authorization',
'sessionid',
'authtoken',
'x-api-key',
'tfa_token',
'otp',
'security_code',
'bearer',
'cookie',
];
return sensitiveTerms.some((term) => content.includes(term));
} catch {
return false;
}
}
}
export { CLIErrorHandler };