-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathboot-diagnostics.ts
More file actions
67 lines (63 loc) · 2.28 KB
/
boot-diagnostics.ts
File metadata and controls
67 lines (63 loc) · 2.28 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
import { asAppError } from '../utils/errors.ts';
export type BootFailureReason =
| 'BOOT_TIMEOUT'
| 'DEVICE_UNAVAILABLE'
| 'DEVICE_OFFLINE'
| 'PERMISSION_DENIED'
| 'TOOL_MISSING'
| 'BOOT_COMMAND_FAILED'
| 'UNKNOWN';
export function classifyBootFailure(input: {
error?: unknown;
message?: string;
stdout?: string;
stderr?: string;
}): BootFailureReason {
const appErr = input.error ? asAppError(input.error) : null;
if (appErr?.code === 'TOOL_MISSING') return 'TOOL_MISSING';
const details = (appErr?.details ?? {}) as Record<string, unknown>;
const detailMessage = typeof details.message === 'string' ? details.message : undefined;
const detailStdout = typeof details.stdout === 'string' ? details.stdout : undefined;
const detailStderr = typeof details.stderr === 'string' ? details.stderr : undefined;
const nestedBoot = details.boot && typeof details.boot === 'object'
? (details.boot as Record<string, unknown>)
: null;
const nestedBootstatus = details.bootstatus && typeof details.bootstatus === 'object'
? (details.bootstatus as Record<string, unknown>)
: null;
const haystack = [
input.message,
appErr?.message,
input.stdout,
input.stderr,
detailMessage,
detailStdout,
detailStderr,
typeof nestedBoot?.stdout === 'string' ? nestedBoot.stdout : undefined,
typeof nestedBoot?.stderr === 'string' ? nestedBoot.stderr : undefined,
typeof nestedBootstatus?.stdout === 'string' ? nestedBootstatus.stdout : undefined,
typeof nestedBootstatus?.stderr === 'string' ? nestedBootstatus.stderr : undefined,
]
.filter(Boolean)
.join('\n')
.toLowerCase();
if (haystack.includes('timed out') || haystack.includes('timeout')) return 'BOOT_TIMEOUT';
if (
haystack.includes('device not found') ||
haystack.includes('no devices') ||
haystack.includes('unable to locate device') ||
haystack.includes('invalid device')
) {
return 'DEVICE_UNAVAILABLE';
}
if (haystack.includes('offline')) return 'DEVICE_OFFLINE';
if (
haystack.includes('permission denied') ||
haystack.includes('not authorized') ||
haystack.includes('unauthorized')
) {
return 'PERMISSION_DENIED';
}
if (appErr?.code === 'COMMAND_FAILED' || haystack.length > 0) return 'BOOT_COMMAND_FAILED';
return 'UNKNOWN';
}