Skip to content

Commit 94ed1bd

Browse files
committed
Fix TypeScript narrowing in boot diagnostics error paths
1 parent c03ace3 commit 94ed1bd

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

src/platforms/android/devices.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { runCmd, whichCmd } from '../../utils/exec.ts';
2+
import type { ExecResult } from '../../utils/exec.ts';
23
import { AppError, asAppError } from '../../utils/errors.ts';
34
import type { DeviceInfo } from '../../utils/device.ts';
45
import { Deadline, retryWithPolicy } from '../../utils/retry.ts';
@@ -63,7 +64,7 @@ export async function isAndroidBooted(serial: string): Promise<boolean> {
6364
export async function waitForAndroidBoot(serial: string, timeoutMs = 60000): Promise<void> {
6465
const deadline = Deadline.fromTimeoutMs(timeoutMs);
6566
const maxAttempts = Math.max(1, Math.ceil(timeoutMs / 1000));
66-
let lastBootResult: { stdout: string; stderr: string; exitCode: number } | null = null;
67+
let lastBootResult: ExecResult | undefined;
6768
let timedOut = false;
6869
try {
6970
await retryWithPolicy(
@@ -109,19 +110,22 @@ export async function waitForAndroidBoot(serial: string, timeoutMs = 60000): Pro
109110
);
110111
} catch (error) {
111112
const appErr = asAppError(error);
113+
const stdout = lastBootResult?.stdout;
114+
const stderr = lastBootResult?.stderr;
115+
const exitCode = lastBootResult?.exitCode;
112116
const reason = classifyBootFailure({
113117
error,
114-
stdout: lastBootResult?.stdout,
115-
stderr: lastBootResult?.stderr,
118+
stdout,
119+
stderr,
116120
});
117121
const baseDetails = {
118122
serial,
119123
timeoutMs,
120124
elapsedMs: deadline.elapsedMs(),
121125
reason,
122-
stdout: lastBootResult?.stdout,
123-
stderr: lastBootResult?.stderr,
124-
exitCode: lastBootResult?.exitCode,
126+
stdout,
127+
stderr,
128+
exitCode,
125129
};
126130
if (timedOut || reason === 'BOOT_TIMEOUT') {
127131
throw new AppError('COMMAND_FAILED', 'Android device did not finish booting in time', baseDetails);

src/platforms/ios/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { runCmd } from '../../utils/exec.ts';
2+
import type { ExecResult } from '../../utils/exec.ts';
23
import { AppError } from '../../utils/errors.ts';
34
import type { DeviceInfo } from '../../utils/device.ts';
45
import { Deadline, retryWithPolicy } from '../../utils/retry.ts';
@@ -212,8 +213,8 @@ export async function ensureBootedSimulator(device: DeviceInfo): Promise<void> {
212213
const state = await getSimulatorState(device.id);
213214
if (state === 'Booted') return;
214215
const deadline = Deadline.fromTimeoutMs(IOS_BOOT_TIMEOUT_MS);
215-
let bootResult: { stdout: string; stderr: string; exitCode: number } | null = null;
216-
let bootStatusResult: { stdout: string; stderr: string; exitCode: number } | null = null;
216+
let bootResult: ExecResult | undefined;
217+
let bootStatusResult: ExecResult | undefined;
217218
try {
218219
await retryWithPolicy(
219220
async () => {
@@ -264,10 +265,16 @@ export async function ensureBootedSimulator(device: DeviceInfo): Promise<void> {
264265
{ deadline },
265266
);
266267
} catch (error) {
268+
const bootStdout = bootResult?.stdout;
269+
const bootStderr = bootResult?.stderr;
270+
const bootExitCode = bootResult?.exitCode;
271+
const bootstatusStdout = bootStatusResult?.stdout;
272+
const bootstatusStderr = bootStatusResult?.stderr;
273+
const bootstatusExitCode = bootStatusResult?.exitCode;
267274
const reason = classifyBootFailure({
268275
error,
269-
stdout: bootStatusResult?.stdout ?? bootResult?.stdout,
270-
stderr: bootStatusResult?.stderr ?? bootResult?.stderr,
276+
stdout: bootstatusStdout ?? bootStdout,
277+
stderr: bootstatusStderr ?? bootStderr,
271278
});
272279
throw new AppError('COMMAND_FAILED', 'iOS simulator failed to boot', {
273280
platform: 'ios',
@@ -276,13 +283,13 @@ export async function ensureBootedSimulator(device: DeviceInfo): Promise<void> {
276283
elapsedMs: deadline.elapsedMs(),
277284
reason,
278285
boot: bootResult
279-
? { exitCode: bootResult.exitCode, stdout: bootResult.stdout, stderr: bootResult.stderr }
286+
? { exitCode: bootExitCode, stdout: bootStdout, stderr: bootStderr }
280287
: undefined,
281288
bootstatus: bootStatusResult
282289
? {
283-
exitCode: bootStatusResult.exitCode,
284-
stdout: bootStatusResult.stdout,
285-
stderr: bootStatusResult.stderr,
290+
exitCode: bootstatusExitCode,
291+
stdout: bootstatusStdout,
292+
stderr: bootstatusStderr,
286293
}
287294
: undefined,
288295
});

0 commit comments

Comments
 (0)