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 {
|
||||
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 {
|
||||
if (this._sources.has(name)) {
|
||||
return false;
|
||||
|
@ -49,11 +55,15 @@ export class PeerDiscovery {
|
|||
}
|
||||
|
||||
for (const source of this._sources.values()) {
|
||||
try {
|
||||
const result = await source(pubkey as Buffer, options);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
} catch (e) {
|
||||
this._logger(e);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue