Gracefully handle socket closures

Summary:
Got my first auto-created crashbot task. {emoji:1f973}

We do have some pretty granular handling for errors during disconnects but this one has fallen through the cracks.

I'm not 100% sure if this is the right way to handle it which is why I added mweststrate. :)

Reviewed By: mweststrate

Differential Revision: D30218833

fbshipit-source-id: 2b4c9201ee7faf1c278b1cc5268ad2648dc4c820
This commit is contained in:
Pascal Hartig
2021-08-10 16:10:57 -07:00
committed by Facebook GitHub Bot
parent 8d7caa9dd4
commit dd536b9d1a

View File

@@ -710,11 +710,22 @@ export default class Client extends EventEmitter {
api,
method,
params,
}).catch((err) => {
}).catch((err: Error) => {
// We only throw errors if the connection is still alive
// as connection-related ones aren't recoverable from
// user code.
if (this.connected.get()) {
// This is a special case where we a send failed because of
// a disconnect "mid-air". This can happen, for instance,
// when you pull the plug from a connected phone. We can
// still handle this gracefully.
if (err.toString().includes('Socket closed unexpectedly')) {
console.warn(
`Failed to call device due to unexpected disconnect: ${err}`,
);
this.disconnect();
return {};
}
throw err;
}
// This effectively preserves the previous behavior