From 6d98acce1dd04e5bd1795a372d74058129b10721 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 12 Jul 2023 05:17:32 -0400 Subject: [PATCH] fix: use try catch and use local logger function if there is an error --- src/index.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index c539f9f..76a849c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,12 @@ export type PeerSource = ( export class PeerDiscovery { private _sources: Map = new Map(); + private _logger = console.log; + + set logger(value: (...data: any[]) => void) { + this._logger = value; + } + public registerSource(name: string, source: PeerSource): boolean { if (this._sources.has(name)) { return false; @@ -49,10 +55,14 @@ export class PeerDiscovery { } for (const source of this._sources.values()) { - const result = await source(pubkey as Buffer, options); + try { + const result = await source(pubkey as Buffer, options); - if (result) { - return result; + if (result) { + return result; + } + } catch (e) { + this._logger(e); } }