fix: catch top level exception when preemptively creating streams

This commit is contained in:
fryorcraken.eth 2023-09-20 15:33:13 +10:00
parent 4a9360d4e3
commit fb37c89e40
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 7 additions and 4 deletions

View File

@ -6,8 +6,8 @@ import { selectConnection } from "@waku/utils/libp2p";
import debug from "debug"; import debug from "debug";
export class StreamManager { export class StreamManager {
private streamPool: Map<string, Promise<Stream>>; private streamPool: Map<string, Promise<Stream | void>>;
private log: debug.Debugger; private readonly log: debug.Debugger;
constructor( constructor(
public multicodec: string, public multicodec: string,
@ -38,7 +38,7 @@ export class StreamManager {
const stream = await streamPromise; const stream = await streamPromise;
if (stream.status === "closed") { if (!stream || stream.status === "closed") {
return this.newStream(peer); // fallback by creating a new stream on the spot return this.newStream(peer); // fallback by creating a new stream on the spot
} }
@ -55,7 +55,10 @@ export class StreamManager {
} }
private prepareNewStream(peer: Peer): void { private prepareNewStream(peer: Peer): void {
const streamPromise = this.newStream(peer); const streamPromise = this.newStream(peer).catch(() => {
// No error thrown as this call is not triggered by the user
this.log(`Failed to prepare a new stream for ${peer.id.toString()}`);
});
this.streamPool.set(peer.id.toString(), streamPromise); this.streamPool.set(peer.id.toString(), streamPromise);
} }