Skip to content

Commit efd3d2a

Browse files
committed
docs: adding commentary for .catch() usage for ignoring errors
[ci skip]
1 parent 70c20e0 commit efd3d2a

6 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/rpc/RPCClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ class RPCClient<M extends ClientManifest> {
376376
const signal = abortController.signal;
377377
// A promise that will reject if there is an abort signal or timeout
378378
const abortRaceProm = promise<never>();
379+
// Prevent unhandled rejection when we're don with the promise
379380
abortRaceProm.p.catch(() => {});
380381
let abortHandler: () => void;
381382
if (ctx.signal != null) {

src/rpc/RPCServer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ class RPCServer extends EventTarget {
335335
await outputGenerator.return(undefined);
336336
},
337337
});
338+
// Ignore any errors here, it should propagate to the ends of the stream
338339
void reverseMiddlewareStream.pipeTo(reverseStream).catch(() => {});
339340
return middleware.reverse.readable;
340341
};
@@ -515,7 +516,7 @@ class RPCServer extends EventTarget {
515516
);
516517
const outputStreamEndProm = outputStream
517518
.pipeTo(rpcStream.writable)
518-
.catch(() => {});
519+
.catch(() => {}); // Ignore any errors, we only care that it finished
519520
await Promise.allSettled([inputStreamEndProm, outputStreamEndProm]);
520521
// Cleaning up abort and timer
521522
timer.cancel(cleanupReason);

src/rpc/handlers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ abstract class Handler<
99
Input extends JSONValue = JSONValue,
1010
Output extends JSONValue = JSONValue,
1111
> {
12+
// These are used to distinguish the handlers in the type system.
13+
// Without these the map types can't tell the types of handlers apart.
1214
protected _inputType: Input;
1315
protected _outputType: Output;
16+
/**
17+
* This is the timeout used for the handler.
18+
* If it is not set then the default timeout time for the `RPCServer` is used.
19+
*/
1420
public timeout?: number;
1521

1622
constructor(protected container: Container) {}

src/rpc/utils/middleware.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ function defaultMiddleware() {
9595
* The reverse path will pipe the output stream through the provided middleware
9696
* and then transform it back to a binary stream.
9797
* @param middlewareFactory - The provided middleware
98-
* @param ctx
9998
*/
10099
function defaultServerMiddlewareWrapper(
101100
middlewareFactory: MiddlewareFactory<

src/websockets/WebSocketClient.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ class WebSocketClient {
104104
}
105105
}
106106
for (const activeConnection of this.activeConnections) {
107-
await activeConnection.endedProm.catch(() => {}); // Ignore errors here
107+
// Ignore errors here, we only care that it finishes
108+
await activeConnection.endedProm.catch(() => {});
108109
}
109110
this.logger.info(`Destroyed ${this.constructor.name}`);
110111
}
@@ -117,7 +118,8 @@ class WebSocketClient {
117118
);
118119
}
119120
for (const activeConnection of this.activeConnections) {
120-
await activeConnection.endedProm.catch(() => {}); // Ignore errors here
121+
// Ignore errors here, we only care that it finished
122+
await activeConnection.endedProm.catch(() => {});
121123
}
122124
}
123125

@@ -134,14 +136,14 @@ class WebSocketClient {
134136
new Timer({
135137
delay: this.connectionTimeoutTime,
136138
});
137-
void timer.catch(() => {});
138-
void timer
139-
.then(() => {
139+
void timer.then(
140+
() => {
140141
abortRaceProm.rejectP(
141142
new webSocketErrors.ErrorClientConnectionTimedOut(),
142143
);
143-
})
144-
.catch(() => {});
144+
},
145+
() => {}, // Ignore cancellation errors
146+
);
145147
const { signal } = ctx;
146148
let abortHandler: () => void | undefined;
147149
if (signal != null) {
@@ -242,7 +244,8 @@ class WebSocketClient {
242244
// Setting up activeStream map lifecycle
243245
this.activeConnections.add(webSocketStreamClient);
244246
void webSocketStreamClient.endedProm
245-
.catch(() => {}) // Ignore errors
247+
// Ignore errors, we only care that it finished
248+
.catch(() => {})
246249
.finally(() => {
247250
this.activeConnections.delete(webSocketStreamClient);
248251
signal?.removeEventListener('abort', abortStream);

src/websockets/WebSocketServer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ class WebSocketServer extends EventTarget {
222222
}
223223
// Wait for all active websockets to close
224224
for (const webSocketStream of this.activeSockets) {
225-
webSocketStream.endedProm.catch(() => {}); // Ignore errors
225+
// Ignore errors, we only care that it finished
226+
webSocketStream.endedProm.catch(() => {});
226227
}
227228
if (this.connectionEventHandler != null) {
228229
this.removeEventListener('connection', this.connectionEventHandler);
@@ -303,7 +304,8 @@ class WebSocketServer extends EventTarget {
303304
// Adding socket to the active sockets map
304305
this.activeSockets.add(webSocketStream);
305306
webSocketStream.endedProm
306-
.catch(() => {}) // Ignore errors here
307+
// Ignore errors, we only care that it finished
308+
.catch(() => {})
307309
.finally(() => {
308310
this.activeSockets.delete(webSocketStream);
309311
});

0 commit comments

Comments
 (0)