Skip to content

Commit

Permalink
Use detailed usbmux error class for failures and drop logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Jun 14, 2024
1 parent 1da43ed commit 51a6f64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@httptoolkit/util": "^0.1.2",
"@httptoolkit/util": "^0.1.5",
"plist": "^3.1.0"
}
}
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as net from 'net';
import * as plist from 'plist';
import { delay, getDeferred } from '@httptoolkit/util';
import { delay, getDeferred, CustomError } from '@httptoolkit/util';

import { readBytes } from './stream-utils';

Expand Down Expand Up @@ -165,6 +165,22 @@ export interface DeviceInfo {
SerialNumber?: string;
}

export class UsbmuxError extends CustomError {

constructor(operation: string, response: ResponseMessage | null) {
const failureType = response === null
? 'usbmux-connection-failure'
: response.MessageType !== 'Result'
? `usbmux-unexpected-${response.MessageType}-message`
: `usbmux-unexpected-${response.Number}-result`;

super(`Usbmux ${operation} request failed: ${failureType}`, {
code: failureType
});
}

}

export class UsbmuxClient {

constructor(
Expand Down Expand Up @@ -201,7 +217,7 @@ export class UsbmuxClient {
response.MessageType !== 'Result' ||
response.Number !== 0
) {
throw new Error('Usbmux connection failed');
throw new UsbmuxError('listen', response);
};

this.listenToMessages(conn);
Expand Down Expand Up @@ -279,8 +295,7 @@ export class UsbmuxClient {
response.MessageType !== 'Result' ||
response.Number !== 0
) {
console.warn(`Unexpected usbmux response`, response);
throw new Error('Usbmux connection failed');
throw new UsbmuxError('tunnel', response);
};

return conn;
Expand Down

0 comments on commit 51a6f64

Please sign in to comment.