Skip to content

Commit

Permalink
feat(android): better error messaging
Browse files Browse the repository at this point in the history
Catch the awful "ERR_UNSUITABLE_API_INSTALLATION" error during run and
print something a bit more friendly.

resolves #7
  • Loading branch information
imhoffd committed May 30, 2019
1 parent e4e4ba2 commit 0cfa51a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/android/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { CLIException, ERR_BAD_INPUT, ERR_TARGET_NOT_FOUND, RunException } from '../errors';
import * as Debug from 'debug';

import { AVDException, CLIException, ERR_BAD_INPUT, ERR_NO_DEVICE, ERR_NO_TARGET, ERR_TARGET_NOT_FOUND, ERR_UNSUITABLE_API_INSTALLATION, RunException } from '../errors';
import { getOptionValue, getOptionValues } from '../utils/cli';
import { log } from '../utils/log';
import { onBeforeExit } from '../utils/process';
Expand All @@ -9,6 +11,8 @@ import { getInstalledAVDs } from './utils/avd';
import { installApkToDevice, selectDeviceByTarget, selectHardwareDevice, selectVirtualDevice } from './utils/run';
import { SDK, getSDK } from './utils/sdk';

const modulePrefix = 'native-run:android:run';

export async function run(args: ReadonlyArray<string>): Promise<void> {
const sdk = await getSDK();
const apkPath = getOptionValue(args, '--app');
Expand Down Expand Up @@ -72,6 +76,8 @@ export async function run(args: ReadonlyArray<string>): Promise<void> {
}

export async function selectDevice(sdk: SDK, args: ReadonlyArray<string>): Promise<Device> {
const debug = Debug(`${modulePrefix}:${selectDevice.name}`);

const devices = await getDevices(sdk);
const avds = await getInstalledAVDs(sdk);

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

if (selectedDevice) {
return selectedDevice;
} else if (args.includes('--device')) {
throw new RunException(`No hardware devices found. Not attempting emulator because --device was specified.`, ERR_NO_DEVICE);
} else {
log('No hardare devices found, attempting emulator...\n');
}
}

try {
return await selectVirtualDevice(sdk, devices, avds);
} catch (e) {
if (!(e instanceof AVDException)) {
throw e;
}

debug('Issue with AVDs: %s', e.message);

if (e.code === ERR_UNSUITABLE_API_INSTALLATION) {
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);
}
}

return selectVirtualDevice(sdk, devices, avds);
throw new RunException('No targets available.', ERR_NO_TARGET);
}
6 changes: 5 additions & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const ERR_SDK_NOT_FOUND = 'ERR_SDK_NOT_FOUND';
export const ERR_SDK_PACKAGE_NOT_FOUND = 'ERR_SDK_PACKAGE_NOT_FOUND';
export const ERR_SDK_UNSATISFIED_PACKAGES = 'ERR_SDK_UNSATISFIED_PACKAGES';
export const ERR_TARGET_NOT_FOUND = 'ERR_TARGET_NOT_FOUND';
export const ERR_NO_DEVICE = 'ERR_NO_DEVICE';
export const ERR_NO_TARGET = 'ERR_NO_TARGET';
export const ERR_UNKNOWN_AVD = 'ERR_UNKNOWN_AVD';
export const ERR_UNSUPPORTED_API_LEVEL = 'ERR_UNSUPPORTED_API_LEVEL';

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

export type RunExceptionCode = (
typeof ERR_NO_AVDS_FOUND |
typeof ERR_TARGET_NOT_FOUND
typeof ERR_TARGET_NOT_FOUND |
typeof ERR_NO_DEVICE |
typeof ERR_NO_TARGET
);

export class RunException extends Exception<RunExceptionCode> {}
Expand Down

0 comments on commit 0cfa51a

Please sign in to comment.