From e6527e9ab18eeb880ae94c5c970d01b8b1c193df Mon Sep 17 00:00:00 2001 From: Danish Arora <35004822+danisharora099@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:33:42 +0530 Subject: [PATCH] chore: add error handling for an uncaught dial attempt (#1660) --- packages/core/src/lib/connection_manager.ts | 28 +++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/core/src/lib/connection_manager.ts b/packages/core/src/lib/connection_manager.ts index 859b64813a..1401a967be 100644 --- a/packages/core/src/lib/connection_manager.ts +++ b/packages/core/src/lib/connection_manager.ts @@ -226,16 +226,24 @@ export class ConnectionManager try { const error = this.dialErrorsForPeer.get(peerId.toString()); - let errorMessage; - if (error instanceof AggregateError) { - errorMessage = JSON.stringify(error.errors[0]); - } else { - errorMessage = error.message; - } + if (error) { + let errorMessage; + if (error instanceof AggregateError) { + if (!error.errors) { + log(`No errors array found for AggregateError`); + } else if (error.errors.length === 0) { + log(`Errors array is empty for AggregateError`); + } else { + errorMessage = JSON.stringify(error.errors[0]); + } + } else { + errorMessage = error.message; + } - log( - `Deleting undialable peer ${peerId.toString()} from peer store. Error: ${errorMessage}` - ); + log( + `Deleting undialable peer ${peerId.toString()} from peer store. Error: ${errorMessage}` + ); + } this.dialErrorsForPeer.delete(peerId.toString()); await this.libp2p.peerStore.delete(peerId); @@ -314,7 +322,7 @@ export class ConnectionManager if (!(await this.shouldDialPeer(peerId))) return; this.dialPeer(peerId).catch((err) => { - throw `Error dialing peer ${peerId.toString()} : ${err}`; + log(`Error dialing peer ${peerId.toString()} : ${err}`); }); }