|
| 1 | +type AndroidAdbFailureReason = |
| 2 | + | 'timeout' |
| 3 | + | 'device_offline' |
| 4 | + | 'device_unauthorized' |
| 5 | + | 'device_not_found' |
| 6 | + | 'multiple_devices' |
| 7 | + | 'no_devices' |
| 8 | + | 'connection_dropped' |
| 9 | + | 'server_version_mismatch' |
| 10 | + | 'install_insufficient_storage' |
| 11 | + | 'install_update_incompatible' |
| 12 | + | 'install_version_downgrade' |
| 13 | + | 'install_failed'; |
| 14 | + |
| 15 | +export type AndroidAdbFailureClassification = Readonly<{ |
| 16 | + /** Machine-readable failure family attached to error details as `adbFailure`. */ |
| 17 | + reason: AndroidAdbFailureReason; |
| 18 | + hint: string; |
| 19 | + /** Present only when an unchanged retry can succeed. */ |
| 20 | + retriable?: boolean; |
| 21 | +}>; |
| 22 | + |
| 23 | +type AndroidAdbFailureMatcher = AndroidAdbFailureClassification & |
| 24 | + Readonly<{ |
| 25 | + pattern: RegExp; |
| 26 | + /** Android package-manager install verdicts may be emitted on stdout. */ |
| 27 | + matchStdout?: boolean; |
| 28 | + }>; |
| 29 | + |
| 30 | +const ANDROID_ADB_FAILURE_MATCHERS: readonly AndroidAdbFailureMatcher[] = [ |
| 31 | + { |
| 32 | + reason: 'device_unauthorized', |
| 33 | + pattern: /device unauthorized|device still authorizing/, |
| 34 | + hint: 'USB debugging is not authorized — accept the authorization prompt on the device screen (re-plug the cable if none appears), then retry.', |
| 35 | + }, |
| 36 | + { |
| 37 | + reason: 'device_offline', |
| 38 | + pattern: /device offline/, |
| 39 | + hint: 'The device is connected but offline — wait for it to finish booting or run adb reconnect, then retry.', |
| 40 | + retriable: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + reason: 'multiple_devices', |
| 44 | + pattern: /more than one (?:device\/emulator|device and emulator)/, |
| 45 | + hint: 'Multiple Android devices are connected — pass --serial <serial> (see adb devices) to select one.', |
| 46 | + }, |
| 47 | + { |
| 48 | + reason: 'no_devices', |
| 49 | + pattern: /no devices\/emulators found|no devices found/, |
| 50 | + hint: 'No Android devices detected — boot an emulator or connect a device and verify it appears in adb devices.', |
| 51 | + }, |
| 52 | + { |
| 53 | + reason: 'device_not_found', |
| 54 | + pattern: /device (?:'[^']*' )?not found/, |
| 55 | + hint: 'The device disconnected or is restarting — verify it is listed in adb devices, then retry.', |
| 56 | + retriable: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + reason: 'server_version_mismatch', |
| 60 | + pattern: /adb server version \(\d+\) doesn't match this client/, |
| 61 | + hint: 'Multiple adb installs conflict — adb restarts its server automatically, so retry; align PATH to a single adb to stop recurrences.', |
| 62 | + retriable: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + reason: 'connection_dropped', |
| 66 | + pattern: /transport error|connection reset|broken pipe|protocol fault/, |
| 67 | + hint: 'The adb connection dropped — retry; if it persists, run adb kill-server and reconnect the device.', |
| 68 | + retriable: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + reason: 'install_insufficient_storage', |
| 72 | + pattern: /install_failed_insufficient_storage/, |
| 73 | + hint: 'The device is out of storage — free up space or uninstall unused apps, then retry the install.', |
| 74 | + matchStdout: true, |
| 75 | + }, |
| 76 | + { |
| 77 | + reason: 'install_update_incompatible', |
| 78 | + pattern: /install_failed_update_incompatible/, |
| 79 | + hint: 'The installed app has an incompatible signature — uninstall the existing app first, then retry the install.', |
| 80 | + matchStdout: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + reason: 'install_version_downgrade', |
| 84 | + pattern: /install_failed_version_downgrade/, |
| 85 | + hint: 'The APK is older than the installed app — uninstall the app first (or install with downgrade allowed), then retry.', |
| 86 | + matchStdout: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + reason: 'install_failed', |
| 90 | + pattern: /install_failed_\w+|install_parse_failed_\w+/, |
| 91 | + hint: 'The Android package installer rejected the APK — see the INSTALL_FAILED code in the error output for the exact cause.', |
| 92 | + matchStdout: true, |
| 93 | + }, |
| 94 | +]; |
| 95 | + |
| 96 | +export const ANDROID_ADB_TIMEOUT_FAILURE: AndroidAdbFailureClassification = Object.freeze({ |
| 97 | + reason: 'timeout', |
| 98 | + hint: 'adb timed out — the adb server may be wedged. Run adb kill-server && adb start-server, check adb devices, then retry.', |
| 99 | +}); |
| 100 | + |
| 101 | +/** |
| 102 | + * Classifies transport failures from stderr. Package-manager install verdicts |
| 103 | + * additionally inspect stdout because adb emits those semantic results there. |
| 104 | + */ |
| 105 | +export function classifyAndroidAdbFailure( |
| 106 | + stderr: string, |
| 107 | + stdout = '', |
| 108 | +): AndroidAdbFailureClassification | undefined { |
| 109 | + const stderrText = stderr.toLowerCase(); |
| 110 | + const stdoutText = stdout.toLowerCase(); |
| 111 | + for (const { pattern, matchStdout, ...classification } of ANDROID_ADB_FAILURE_MATCHERS) { |
| 112 | + if (pattern.test(stderrText) || (matchStdout && pattern.test(stdoutText))) { |
| 113 | + return classification; |
| 114 | + } |
| 115 | + } |
| 116 | + return undefined; |
| 117 | +} |
0 commit comments