Skip to content

Commit cdc6cf0

Browse files
authored
Added the correct exception handling of the Android package manager, made the deletion of the installation file mandatory, despite the installation result, by placing it in the finally block. (#403)
Signed-off-by: Кинчаров Данил <[email protected]> Signed-off-by: Кинчаров Данил <[email protected]>
1 parent 47833c5 commit cdc6cf0

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/adb/DeviceClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export default class DeviceClient {
331331
332332
* @param port The port number to connect to.
333333
* @param host Optional. The host to connect to. Allegedly this is supposed to establish a connection to the given host from the device, but we have not been able to get it to work at all. Skip the host and everything works great.
334-
*
334+
*
335335
* @returns The TCP connection (i.e. [`net.Socket`][node-net]). Read and write as you please. Call `conn.end()` to end the connection.
336336
*/
337337
public openTcp(port: number, host?: string): Bluebird<Duplex> {
@@ -453,8 +453,9 @@ export default class DeviceClient {
453453
return this.transport().then((transport) => {
454454
return new InstallCommand(transport)
455455
.execute(apk)
456-
.then(() => this.shell(['rm', '-f', apk]))
457-
.then((stream) => new Parser(stream).readAll())
456+
.finally(() => {
457+
return this.shell(['rm', '-f', apk]).then((stream) => new Parser(stream).readAll());
458+
})
458459
.then(() => true);
459460
});
460461
}
@@ -525,7 +526,7 @@ export default class DeviceClient {
525526
* Retrieves information about the given path.
526527
*
527528
* @param path The path.
528-
*
529+
*
529530
* @returns An [`fs.Stats`][node-fs-stats] instance. While the `stats.is*` methods are available, only the following properties are supported:
530531
- **mode** The raw mode.
531532
- **size** The file size.

src/adb/command/host-transport/install.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,38 @@ import Protocol from '../../protocol';
22
import Command from '../../command';
33
import Bluebird from 'bluebird';
44

5+
const OKAY_OUTPUT_REGEXP = /^(Success|Failure \[(.*?)\]|Exception)(.*)$/;
6+
const INSTALL_EXCEPTION_CODE = 'INSTALL_EXCEPTION';
7+
8+
class InstallError extends Error {
9+
code: string;
10+
constructor(message: string, code: string) {
11+
super(message);
12+
this.code = code;
13+
}
14+
}
15+
516
export default class InstallCommand extends Command<boolean> {
617
execute(apk: string): Bluebird<boolean> {
718
this._send(`shell:pm install -r ${this._escapeCompat(apk)}`);
819
return this.parser.readAscii(4).then((reply) => {
920
switch (reply) {
1021
case Protocol.OKAY:
1122
return this.parser
12-
.searchLine(/^(Success|Failure \[(.*?)\])$/)
13-
.then(function (match) {
14-
let code, err;
23+
.searchLine(OKAY_OUTPUT_REGEXP)
24+
.then((match) => {
1525
if (match[1] === 'Success') {
1626
return true;
27+
} else if (match[1] === 'Exception') {
28+
return this.parser.readLine().then((buffer: Buffer) => {
29+
throw new InstallError(buffer.toString(), INSTALL_EXCEPTION_CODE);
30+
});
1731
} else {
18-
code = match[2];
19-
err = new Error(`${apk} could not be installed [${code}]`);
20-
err.code = code;
21-
throw err;
32+
const code = match[2];
33+
throw new InstallError(`${apk} could not be installed [${code}]`, code);
2234
}
2335
})
2436
.finally(() => {
25-
// Consume all remaining content to "naturally" close the
26-
// connection.
2737
return this.parser.readAll();
2838
});
2939
case Protocol.FAIL:

0 commit comments

Comments
 (0)