forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorClassifier.ts
More file actions
53 lines (45 loc) · 1.46 KB
/
errorClassifier.ts
File metadata and controls
53 lines (45 loc) · 1.46 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
import { CancellationError } from 'vscode';
import { RpcTimeoutError } from '../../managers/common/nativePythonFinder';
export type DiscoveryErrorType =
| 'spawn_timeout'
| 'spawn_enoent'
| 'permission_denied'
| 'canceled'
| 'parse_error'
| 'unknown';
/**
* Classifies an error into a telemetry-safe category for the `errorType` property.
* Does NOT include raw error messages — only the category.
*/
export function classifyError(ex: unknown): DiscoveryErrorType {
if (ex instanceof CancellationError) {
return 'canceled';
}
if (ex instanceof RpcTimeoutError) {
return 'spawn_timeout';
}
if (!(ex instanceof Error)) {
return 'unknown';
}
// Check error code for spawn failures (Node.js sets `code` on spawn errors)
const code = (ex as NodeJS.ErrnoException).code;
if (code === 'ENOENT') {
return 'spawn_enoent';
}
if (code === 'EACCES' || code === 'EPERM') {
return 'permission_denied';
}
// Check message patterns
const msg = ex.message.toLowerCase();
if (msg.includes('timed out') || msg.includes('timeout')) {
return 'spawn_timeout';
}
if (msg.includes('parse') || msg.includes('unexpected token') || msg.includes('json')) {
return 'parse_error';
}
// Check error name for cancellation variants
if (ex.name === 'CancellationError' || ex.name === 'AbortError') {
return 'canceled';
}
return 'unknown';
}