fix: use try catch and use local logger function if there is an error

This commit is contained in:
Derrick Hammer 2023-07-12 05:17:32 -04:00
parent d3a991d998
commit 6d98acce1d
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 13 additions and 3 deletions

View File

@ -13,6 +13,12 @@ export type PeerSource = (
export class PeerDiscovery { export class PeerDiscovery {
private _sources: Map<string, PeerSource> = new Map<string, PeerSource>(); private _sources: Map<string, PeerSource> = new Map<string, PeerSource>();
private _logger = console.log;
set logger(value: (...data: any[]) => void) {
this._logger = value;
}
public registerSource(name: string, source: PeerSource): boolean { public registerSource(name: string, source: PeerSource): boolean {
if (this._sources.has(name)) { if (this._sources.has(name)) {
return false; return false;
@ -49,10 +55,14 @@ export class PeerDiscovery {
} }
for (const source of this._sources.values()) { 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) { if (result) {
return result; return result;
}
} catch (e) {
this._logger(e);
} }
} }