fix: use try catch and use local logger function if there is an error
This commit is contained in:
parent
d3a991d998
commit
6d98acce1d
10
src/index.ts
10
src/index.ts
|
@ -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,11 +55,15 @@ export class PeerDiscovery {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const source of this._sources.values()) {
|
for (const source of this._sources.values()) {
|
||||||
|
try {
|
||||||
const result = await source(pubkey as Buffer, options);
|
const result = await source(pubkey as Buffer, options);
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
this._logger(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in New Issue