Skip to content

Commit

Permalink
[Changed] Optimize error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
puer authored and xinzhengzhang committed Jan 26, 2024
1 parent f773c3a commit 44fc027
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 19 additions & 6 deletions plugin/zxz-moe-bis/pymobiledevicelite/pymobiledevicelite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from installationProxyService import SimplifiedInstallationProxyService
from packaging.version import Version
from pymobiledevice3.cli.cli_common import Command, RSDCommand
from pymobiledevice3.exceptions import DeviceNotFoundError, NoDeviceConnectedError, AppInstallError, TunneldConnectionError
from pymobiledevice3.exceptions import *
from pymobiledevice3.exceptions import AppInstallError
from pymobiledevice3.lockdown import create_using_usbmux, LockdownClient
from pymobiledevice3.tcp_forwarder import LockdownTcpForwarder
from pymobiledevice3.tunneld import TunneldRunner
Expand Down Expand Up @@ -135,12 +136,24 @@ def rsd_info(service_provider: RemoteServiceDiscoveryService):
try:
cli()
except NoDeviceConnectedError as e:
click.echo(json.dumps({"code": -1, "message": "No device connected"}))
click.echo(json.dumps({"code": 100, "message": "No device connected"}))
except DeviceNotFoundError as e:
click.echo(json.dumps({"code": -2, "message": f"Device Not Found: {e.udid}"}))
click.echo(json.dumps({"code": 100, "message": f"Device Not Found: {e.udid}"}))
except AppInstallError as e:
click.echo(json.dumps({"code": -3, "message": f"App Install Error: {e.args}"}))
click.echo(json.dumps({"code": 100, "message": f"App Install Error: {e.args}"}))
except TunneldConnectionError as e:
click.echo(json.dumps({"code": -4, "message": f"Tunneld Connect Failed"}))
click.echo(json.dumps({"code": 100, "message": "Tunneld Connect Failed"}))
except PasswordRequiredError as e:
click.echo(json.dumps({"code": 100, "message": "Device is locked"}))
except DeveloperModeError as e:
click.echo(json.dumps({"code": 100, "message": "amfid failed to enable developer mode"}))
except DeveloperModeIsNotEnabledError as e:
click.echo(json.dumps({"code": 100, "message": "Developer mode is not enabled"}))
except NotTrustedError as e:
click.echo(json.dumps({"code": 100, "message": "Device is not trusted"}))
except PyMobileDevice3Exception as e:
click.echo(json.dumps({"code": 100, "message": f"PyMobileDevice3Exception: {type(e).__name__}"}))
except OSError as e:
click.echo(json.dumps({"code": e.errno, "message": f"OSError: {e.strerror}"}))
except Exception as e:
click.echo(json.dumps({"code": -999, "message": f"Unknown Error: {type(e).__name__}"}))
click.echo(json.dumps({"code": 999, "message": f"Unknown Error: {type(e).__name__}"}))
12 changes: 8 additions & 4 deletions plugin/zxz-moe-bis/src/pymobiledevice3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const bazelExe = configuration.bazelExecutablePath;

const baseArgs = ['run', '//:pymobiledevicelite', '--'];

function handleError(code: number, message: string) {
logger.error(`${message}\nerror code: ${code}`);
throw new Error(`${message}\nerror code: ${code}`);
}

// to check if the specific device is connected via a tunnel or not
async function rsdInfo(udid: string): Promise<{ host: string, port: number } | undefined> {
let args = baseArgs.concat(['rsd-info', '--tunnel', udid]);
Expand Down Expand Up @@ -157,7 +162,7 @@ export async function appPath(udid: string, bundleID: string): Promise<string> {
logger.log(`App path: ${path}`);
resolve(path);
} else {
logger.error(data.value.message);
handleError(data.value.code, data.value.message);
reject(`Could not find app path for ${bundleID}`);
}
});
Expand Down Expand Up @@ -210,7 +215,7 @@ export async function listDevices(): Promise<Device[]> {

resolve(devices);
} else {
logger.error(output.message);
handleError(data.value.code, data.value.message);
resolve([]);
}
});
Expand Down Expand Up @@ -280,8 +285,7 @@ export async function debugserver(device: Device, cancellationToken: { cancel():
if (output.code === 0) {
logger.log(`debug server info:\nHost: ${output.data.host}\nPort: ${output.data.port}\nUsage: ${output.data.usage}`);
} else {
logger.error(output.message);

handleError(data.value.code, data.value.message);
}
resolve({
host: output.data.host,
Expand Down

0 comments on commit 44fc027

Please sign in to comment.