Skip to content

Commit 0cfa51a

Browse files
committed
feat(android): better error messaging
Catch the awful "ERR_UNSUITABLE_API_INSTALLATION" error during run and print something a bit more friendly. resolves #7
1 parent e4e4ba2 commit 0cfa51a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/android/run.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { CLIException, ERR_BAD_INPUT, ERR_TARGET_NOT_FOUND, RunException } from '../errors';
1+
import * as Debug from 'debug';
2+
3+
import { AVDException, CLIException, ERR_BAD_INPUT, ERR_NO_DEVICE, ERR_NO_TARGET, ERR_TARGET_NOT_FOUND, ERR_UNSUITABLE_API_INSTALLATION, RunException } from '../errors';
24
import { getOptionValue, getOptionValues } from '../utils/cli';
35
import { log } from '../utils/log';
46
import { onBeforeExit } from '../utils/process';
@@ -9,6 +11,8 @@ import { getInstalledAVDs } from './utils/avd';
911
import { installApkToDevice, selectDeviceByTarget, selectHardwareDevice, selectVirtualDevice } from './utils/run';
1012
import { SDK, getSDK } from './utils/sdk';
1113

14+
const modulePrefix = 'native-run:android:run';
15+
1216
export async function run(args: ReadonlyArray<string>): Promise<void> {
1317
const sdk = await getSDK();
1418
const apkPath = getOptionValue(args, '--app');
@@ -72,6 +76,8 @@ export async function run(args: ReadonlyArray<string>): Promise<void> {
7276
}
7377

7478
export async function selectDevice(sdk: SDK, args: ReadonlyArray<string>): Promise<Device> {
79+
const debug = Debug(`${modulePrefix}:${selectDevice.name}`);
80+
7581
const devices = await getDevices(sdk);
7682
const avds = await getInstalledAVDs(sdk);
7783

@@ -93,8 +99,26 @@ export async function selectDevice(sdk: SDK, args: ReadonlyArray<string>): Promi
9399

94100
if (selectedDevice) {
95101
return selectedDevice;
102+
} else if (args.includes('--device')) {
103+
throw new RunException(`No hardware devices found. Not attempting emulator because --device was specified.`, ERR_NO_DEVICE);
104+
} else {
105+
log('No hardare devices found, attempting emulator...\n');
106+
}
107+
}
108+
109+
try {
110+
return await selectVirtualDevice(sdk, devices, avds);
111+
} catch (e) {
112+
if (!(e instanceof AVDException)) {
113+
throw e;
114+
}
115+
116+
debug('Issue with AVDs: %s', e.message);
117+
118+
if (e.code === ERR_UNSUITABLE_API_INSTALLATION) {
119+
throw new RunException('No targets available. Cannot create AVD because there is no suitable API installation. Use --sdk-info to reveal missing packages and other issues.', ERR_NO_TARGET);
96120
}
97121
}
98122

99-
return selectVirtualDevice(sdk, devices, avds);
123+
throw new RunException('No targets available.', ERR_NO_TARGET);
100124
}

src/errors.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export const ERR_SDK_NOT_FOUND = 'ERR_SDK_NOT_FOUND';
3434
export const ERR_SDK_PACKAGE_NOT_FOUND = 'ERR_SDK_PACKAGE_NOT_FOUND';
3535
export const ERR_SDK_UNSATISFIED_PACKAGES = 'ERR_SDK_UNSATISFIED_PACKAGES';
3636
export const ERR_TARGET_NOT_FOUND = 'ERR_TARGET_NOT_FOUND';
37+
export const ERR_NO_DEVICE = 'ERR_NO_DEVICE';
38+
export const ERR_NO_TARGET = 'ERR_NO_TARGET';
3739
export const ERR_UNKNOWN_AVD = 'ERR_UNKNOWN_AVD';
3840
export const ERR_UNSUPPORTED_API_LEVEL = 'ERR_UNSUPPORTED_API_LEVEL';
3941

@@ -74,7 +76,9 @@ export class EmulatorException extends Exception<EmulatorExceptionCode> {}
7476

7577
export type RunExceptionCode = (
7678
typeof ERR_NO_AVDS_FOUND |
77-
typeof ERR_TARGET_NOT_FOUND
79+
typeof ERR_TARGET_NOT_FOUND |
80+
typeof ERR_NO_DEVICE |
81+
typeof ERR_NO_TARGET
7882
);
7983

8084
export class RunException extends Exception<RunExceptionCode> {}

0 commit comments

Comments
 (0)