|
| 1 | +import { redactDiagnosticData } from './redaction.ts'; |
| 2 | + |
| 3 | +export type AppErrorCode = |
| 4 | + | 'INVALID_ARGS' |
| 5 | + | 'DEVICE_NOT_FOUND' |
| 6 | + | 'TOOL_MISSING' |
| 7 | + | 'APP_NOT_INSTALLED' |
| 8 | + | 'UNSUPPORTED_PLATFORM' |
| 9 | + | 'UNSUPPORTED_OPERATION' |
| 10 | + | 'COMMAND_FAILED' |
| 11 | + | 'SESSION_NOT_FOUND' |
| 12 | + | 'UNAUTHORIZED' |
| 13 | + | 'UNKNOWN'; |
| 14 | + |
| 15 | +type AppErrorDetails = Record<string, unknown> & { |
| 16 | + hint?: string; |
| 17 | + diagnosticId?: string; |
| 18 | + logPath?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +export type NormalizedError = { |
| 22 | + code: string; |
| 23 | + message: string; |
| 24 | + hint?: string; |
| 25 | + diagnosticId?: string; |
| 26 | + logPath?: string; |
| 27 | + details?: Record<string, unknown>; |
| 28 | +}; |
| 29 | + |
| 30 | +export class AppError extends Error { |
| 31 | + code: AppErrorCode; |
| 32 | + details?: AppErrorDetails; |
| 33 | + cause?: unknown; |
| 34 | + |
| 35 | + constructor(code: AppErrorCode, message: string, details?: AppErrorDetails, cause?: unknown) { |
| 36 | + super(message); |
| 37 | + this.code = code; |
| 38 | + this.details = details; |
| 39 | + this.cause = cause; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export function asAppError(err: unknown): AppError { |
| 44 | + if (err instanceof AppError) return err; |
| 45 | + if (err instanceof Error) { |
| 46 | + return new AppError('UNKNOWN', err.message, undefined, err); |
| 47 | + } |
| 48 | + return new AppError('UNKNOWN', 'Unknown error', { err }); |
| 49 | +} |
| 50 | + |
| 51 | +export function normalizeError( |
| 52 | + err: unknown, |
| 53 | + context: { diagnosticId?: string; logPath?: string } = {}, |
| 54 | +): NormalizedError { |
| 55 | + const appErr = asAppError(err); |
| 56 | + const details = appErr.details ? redactDiagnosticData(appErr.details) : undefined; |
| 57 | + const detailHint = details && typeof details.hint === 'string' ? details.hint : undefined; |
| 58 | + const diagnosticId = |
| 59 | + (details && typeof details.diagnosticId === 'string' ? details.diagnosticId : undefined) ?? |
| 60 | + context.diagnosticId; |
| 61 | + const logPath = |
| 62 | + (details && typeof details.logPath === 'string' ? details.logPath : undefined) ?? |
| 63 | + context.logPath; |
| 64 | + const hint = detailHint ?? defaultHintForCode(appErr.code); |
| 65 | + const cleanDetails = stripDiagnosticMeta(details); |
| 66 | + const message = maybeEnrichCommandFailedMessage(appErr.code, appErr.message, details); |
| 67 | + |
| 68 | + return { |
| 69 | + code: appErr.code, |
| 70 | + message, |
| 71 | + hint, |
| 72 | + diagnosticId, |
| 73 | + logPath, |
| 74 | + details: cleanDetails, |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +function maybeEnrichCommandFailedMessage( |
| 79 | + code: string, |
| 80 | + message: string, |
| 81 | + details: Record<string, unknown> | undefined, |
| 82 | +): string { |
| 83 | + if (code !== 'COMMAND_FAILED') return message; |
| 84 | + if (details?.processExitError !== true) return message; |
| 85 | + const stderr = typeof details?.stderr === 'string' ? details.stderr : ''; |
| 86 | + const excerpt = firstStderrLine(stderr); |
| 87 | + if (!excerpt) return message; |
| 88 | + return excerpt; |
| 89 | +} |
| 90 | + |
| 91 | +function firstStderrLine(stderr: string): string | null { |
| 92 | + const skipPatterns = [ |
| 93 | + /^an error was encountered processing the command/i, |
| 94 | + /^underlying error\b/i, |
| 95 | + /^simulator device failed to complete the requested operation/i, |
| 96 | + ]; |
| 97 | + |
| 98 | + for (const rawLine of stderr.split('\n')) { |
| 99 | + const line = rawLine.trim(); |
| 100 | + if (!line) continue; |
| 101 | + if (skipPatterns.some((pattern) => pattern.test(line))) continue; |
| 102 | + return line.length > 200 ? `${line.slice(0, 200)}...` : line; |
| 103 | + } |
| 104 | + return null; |
| 105 | +} |
| 106 | + |
| 107 | +function stripDiagnosticMeta( |
| 108 | + details: Record<string, unknown> | undefined, |
| 109 | +): Record<string, unknown> | undefined { |
| 110 | + if (!details) return undefined; |
| 111 | + const output = { ...details }; |
| 112 | + delete output.hint; |
| 113 | + delete output.diagnosticId; |
| 114 | + delete output.logPath; |
| 115 | + return Object.keys(output).length > 0 ? output : undefined; |
| 116 | +} |
| 117 | + |
| 118 | +export function defaultHintForCode(code: string): string | undefined { |
| 119 | + switch (code) { |
| 120 | + case 'INVALID_ARGS': |
| 121 | + return 'Check command arguments and run --help for usage examples.'; |
| 122 | + case 'SESSION_NOT_FOUND': |
| 123 | + return 'Run open first or pass an explicit device selector.'; |
| 124 | + case 'TOOL_MISSING': |
| 125 | + return 'Install required platform tooling and ensure it is available in PATH.'; |
| 126 | + case 'DEVICE_NOT_FOUND': |
| 127 | + return 'Verify the target device is booted/connected and selectors match.'; |
| 128 | + case 'APP_NOT_INSTALLED': |
| 129 | + return 'Run apps to discover the exact installed package or bundle id, or install the app before open.'; |
| 130 | + case 'UNSUPPORTED_OPERATION': |
| 131 | + return 'This command is not available for the selected platform/device.'; |
| 132 | + case 'COMMAND_FAILED': |
| 133 | + return 'Retry with --debug and inspect diagnostics log for details.'; |
| 134 | + case 'UNAUTHORIZED': |
| 135 | + return 'Refresh daemon metadata and retry the command.'; |
| 136 | + default: |
| 137 | + return 'Retry with --debug and inspect diagnostics log for details.'; |
| 138 | + } |
| 139 | +} |
0 commit comments