Skip to content

Commit ddade07

Browse files
committed
wip(connect): rework device.run error handling
1 parent 686e0ea commit ddade07

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

packages/connect/src/device/Device.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,32 +351,33 @@ export class Device extends TypedEmitter<DeviceEvents> {
351351
try {
352352
await this.run();
353353
} catch (error) {
354+
_log.warn(`device.run error.message: ${error.message}, code: ${error.code}`);
355+
354356
if (
355357
error.code === 'Device_NotFound' ||
356358
error.message === TRANSPORT_ERROR.DEVICE_NOT_FOUND ||
357359
error.message === TRANSPORT_ERROR.DEVICE_DISCONNECTED_DURING_ACTION ||
358-
error.message === TRANSPORT_ERROR.UNEXPECTED_ERROR ||
359360
error.message === TRANSPORT_ERROR.DESCRIPTOR_NOT_FOUND ||
360361
error.message === TRANSPORT_ERROR.HTTP_ERROR // bridge died during device initialization
361362
) {
362363
// disconnected, do nothing
363364
} else if (
365+
// we don't know what really happened
366+
error.message === TRANSPORT_ERROR.UNEXPECTED_ERROR ||
367+
// someone else took the device at the same time
364368
error.message === TRANSPORT_ERROR.SESSION_WRONG_PREVIOUS ||
365-
error.code === 'Device_UsedElsewhere' // most common error - someone else took the device at the same time
369+
// device had some session when first seen -> we do not read it so that we don't interrupt somebody else's flow
370+
error.code === 'Device_UsedElsewhere' ||
371+
// TODO: is this needed? can't I just use transport error?
372+
error.code === 'Device_InitializeFailed'
366373
) {
367-
// TODO needed only for TRANSPORT_ERROR.SESSION_WRONG_PREVIOUS
368-
// this.enumerate(transport);
369374
this.emitLifecycle(DEVICE.CONNECT_UNACQUIRED);
370375
} else if (
371376
// device was claimed by another application on transport api layer (claimInterface in usb nomenclature) but never released (releaseInterface in usb nomenclature)
372377
// the only remedy for this is to reconnect device manually
373-
// or possibly there are 2 applications without common sessions background
374378
error.message === TRANSPORT_ERROR.INTERFACE_UNABLE_TO_OPEN_DEVICE ||
375379
// catch one of trezord LIBUSB_ERRORs
376-
error.message?.indexOf(ERRORS.LIBUSB_ERROR_MESSAGE) >= 0 ||
377-
// we tried to initialize device (either automatically after enumeration or after user click)
378-
// but it did not work out. this device is effectively unreadable and user should do something about it
379-
error.code === 'Device_InitializeFailed'
380+
error.message?.indexOf(ERRORS.LIBUSB_ERROR_MESSAGE) >= 0
380381
) {
381382
this.unreadableError = error?.message;
382383
this.emitLifecycle(DEVICE.CONNECT_UNACQUIRED);

0 commit comments

Comments
 (0)