From a6f69f2f024c9d9dde494a0dce3f769fdffb5e04 Mon Sep 17 00:00:00 2001 From: Brian Botha Date: Fri, 24 Mar 2023 12:48:14 +1100 Subject: [PATCH] fix: cleaning up temp errors in websocket domain [ci skip] --- src/websockets/WebSocketClient.ts | 34 ++++++++++++++++--------------- src/websockets/WebSocketServer.ts | 10 ++++----- src/websockets/WebSocketStream.ts | 2 +- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/websockets/WebSocketClient.ts b/src/websockets/WebSocketClient.ts index c413c4b0a..d8014b22c 100644 --- a/src/websockets/WebSocketClient.ts +++ b/src/websockets/WebSocketClient.ts @@ -7,8 +7,8 @@ import WebSocket from 'ws'; import { Timer } from '@matrixai/timer'; import { Validator } from 'ip-num'; import WebSocketStream from './WebSocketStream'; -import * as clientRpcUtils from './utils'; -import * as clientRPCErrors from './errors'; +import * as webSocketUtils from './utils'; +import * as webSocketErrors from './errors'; import { promise } from '../utils'; const timeoutSymbol = Symbol('TimedOutSymbol'); @@ -68,7 +68,7 @@ class WebSocketClient { } else if (Validator.isValidIPv6String(host)[0]) { this.host = `[${host}]`; } else { - throw new clientRPCErrors.ErrorClientInvalidHost(); + throw new webSocketErrors.ErrorClientInvalidHost(); } } @@ -85,7 +85,7 @@ class WebSocketClient { this.logger.info(`Destroyed ${this.constructor.name}`); } - @createDestroy.ready(new clientRPCErrors.ErrorClientDestroyed()) + @createDestroy.ready(new webSocketErrors.ErrorClientDestroyed()) public async stopConnections() { for (const activeConnection of this.activeConnections) { activeConnection.end(); @@ -95,7 +95,7 @@ class WebSocketClient { } } - @createDestroy.ready(new clientRPCErrors.ErrorClientDestroyed()) + @createDestroy.ready(new webSocketErrors.ErrorClientDestroyed()) public async startConnection({ timeoutTimer, }: { @@ -119,7 +119,7 @@ class WebSocketClient { // Handle connection failure const openErrorHandler = (e) => { connectProm.rejectP( - new clientRPCErrors.ErrorClientConnectionFailed(undefined, { + new webSocketErrors.ErrorClientConnectionFailed(undefined, { cause: e, }), ); @@ -129,10 +129,10 @@ class WebSocketClient { ws.once('upgrade', async (request) => { const tlsSocket = request.socket as TLSSocket; const peerCert = tlsSocket.getPeerCertificate(true); - clientRpcUtils + webSocketUtils .verifyServerCertificateChain( this.expectedNodeIds, - clientRpcUtils.detailedToCertChain(peerCert), + webSocketUtils.detailedToCertChain(peerCert), ) .then(authenticateProm.resolveP, authenticateProm.rejectP); }); @@ -154,7 +154,7 @@ class WebSocketClient { await Promise.all([authenticateProm.p, connectProm.p]), ]); if (result === timeoutSymbol) { - throw new clientRPCErrors.ErrorClientConnectionTimedOut(); + throw new webSocketErrors.ErrorClientConnectionTimedOut(); } } catch (e) { // Clean up @@ -244,7 +244,7 @@ class WebSocketStreamClientInternal extends WebSocketStream { readableLogger.debug( `Closed early, ${code}, ${reason.toString()}`, ); - const e = new clientRPCErrors.ErrorClientConnectionEndedEarly(); + const e = new webSocketErrors.ErrorClientConnectionEndedEarly(); this.signalReadableEnd(e); controller.error(e); } @@ -289,7 +289,7 @@ class WebSocketStreamClientInternal extends WebSocketStream { ws.once('close', (code, reason) => { if (!this.writableEnded_) { writableLogger.debug(`Closed early, ${code}, ${reason.toString()}`); - const e = new clientRPCErrors.ErrorClientConnectionEndedEarly(); + const e = new webSocketErrors.ErrorClientConnectionEndedEarly(); this.signalWritableEnd(e); controller.error(e); } @@ -304,9 +304,9 @@ class WebSocketStreamClientInternal extends WebSocketStream { ws.close(); } }, - abort: () => { + abort: (reason) => { writableLogger.debug('Aborted'); - this.signalWritableEnd(Error('TMP ABORTED')); + this.signalWritableEnd(reason); if (this.readableEnded_) { writableLogger.debug('Closing socket'); ws.close(); @@ -321,7 +321,7 @@ class WebSocketStreamClientInternal extends WebSocketStream { // Opting to debug message here and not log an error, sending // failure is common if we send before the close event. writableLogger.debug('failed to send'); - const err = new clientRPCErrors.ErrorClientConnectionEndedEarly( + const err = new webSocketErrors.ErrorClientConnectionEndedEarly( undefined, { cause: e, @@ -356,7 +356,9 @@ class WebSocketStreamClientInternal extends WebSocketStream { logger.debug('WebSocket closed'); const err = code !== 1000 - ? Error(`TMP WebSocket ended with code ${code}, ${reason.toString()}`) + ? new webSocketErrors.ErrorClientConnectionEndedEarly( + `ended with code ${code}, ${reason.toString()}`, + ) : undefined; this.signalWebSocketEnd(err); logger.debug('Cleaning up timers'); @@ -367,7 +369,7 @@ class WebSocketStreamClientInternal extends WebSocketStream { } end(): void { - this.ws.close(4001, 'TMP ENDING CONNECTION'); + this.ws.close(4001, 'Ending connection'); } } diff --git a/src/websockets/WebSocketServer.ts b/src/websockets/WebSocketServer.ts index 39c014b24..13c34c543 100644 --- a/src/websockets/WebSocketServer.ts +++ b/src/websockets/WebSocketServer.ts @@ -402,11 +402,11 @@ class WebSocketStreamServerInternal extends WebSocketStream { ws.end(); } }, - abort: () => { + abort: (reason) => { writableLogger.info('Aborted'); if (this.readableEnded_ && !this.webSocketEnded_) { writableLogger.debug('Ending socket'); - this.signalWebSocketEnd(Error('TMP ERROR ABORTED')); + this.signalWebSocketEnd(reason); ws.end(4001, 'ABORTED'); } }, @@ -445,8 +445,8 @@ class WebSocketStreamServerInternal extends WebSocketStream { } }; }, - cancel: () => { - this.signalReadableEnd(Error('TMP READABLE CANCELLED')); + cancel: (reason) => { + this.signalReadableEnd(reason); if (this.writableEnded_ && !this.webSocketEnded_) { readableLogger.debug('Ending socket'); this.signalWebSocketEnd(); @@ -499,7 +499,7 @@ class WebSocketStreamServerInternal extends WebSocketStream { } end(): void { - this.ws.end(4001, 'TMP ENDING CONNECTION'); + this.ws.end(4001, 'Ending connection'); } } diff --git a/src/websockets/WebSocketStream.ts b/src/websockets/WebSocketStream.ts index a91d63a81..604302aa0 100644 --- a/src/websockets/WebSocketStream.ts +++ b/src/websockets/WebSocketStream.ts @@ -36,7 +36,7 @@ abstract class WebSocketStream result[2].status === 'rejected' ) { // Throw a compound error - throw Error('TMP Stream failed', { cause: result }); + throw AggregateError(result, 'stream failed'); } // Otherwise return nothing });