Merge pull request #1098 from nickewansmith/fix-possible-unhandled-throw-on-send

Adds unit test for ripple#1092, fixes unhandled throw on upgraded ws send
This commit is contained in:
Elliot Lee
2020-01-05 19:51:58 -08:00
committed by GitHub
2 changed files with 35 additions and 7 deletions

View File

@@ -475,13 +475,17 @@ class Connection extends EventEmitter {
_send(message: string): Promise<void> {
this._trace('send', message)
return new Promise((resolve, reject) => {
this._ws.send(message, undefined, error => {
if (error) {
reject(new DisconnectedError(error.message, error))
} else {
resolve()
}
})
try {
this._ws.send(message, undefined, error => {
if (error) {
reject(new DisconnectedError(error.message, error))
} else {
resolve()
}
})
} catch (error) {
reject(new DisconnectedError(error.message, error))
}
})
}

View File

@@ -201,6 +201,30 @@ describe('Connection', function() {
})
})
it('DisconnectedError on initial _onOpen send', async function() {
// _onOpen previously could throw PromiseRejectionHandledWarning: Promise rejection was handled asynchronously
// do not rely on the api.setup hook to test this as it bypasses the case, disconnect api connection first
await this.api.disconnect();
// stub _onOpen to only run logic relevant to test case
this.api.connection._onOpen = () => {
// overload websocket send on open when _ws exists
this.api.connection._ws.send = function(data, options, cb) {
// recent ws throws this error instead of calling back
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
}
const request = {command: 'subscribe', streams: ['ledger']};
return this.api.connection.request(request);
}
try {
await this.api.connect();
} catch (error) {
assert(error instanceof this.api.errors.DisconnectedError);
assert.strictEqual(error.message, 'WebSocket is not open: readyState 0 (CONNECTING)');
}
});
it('ResponseFormatError', function() {
this.api.connection._send = function(message) {
const parsed = JSON.parse(message)