Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit f0a4307

Browse files
authored
fix: catch not found errors (#291)
Datastore errors are thrown by some peerstore methods
1 parent 5b84f04 commit f0a4307

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

src/dual-kad-dht.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,25 @@ class DualKadDHT extends EventEmitter {
313313
async getPublicKey (peer, options = {}) {
314314
log('getPublicKey %p', peer)
315315

316+
let peerData
317+
316318
// local check
317-
const peerData = await this._libp2p.peerStore.get(peer)
319+
try {
320+
peerData = await this._libp2p.peerStore.get(peer)
321+
322+
if (peerData.pubKey) {
323+
log('getPublicKey: found local copy')
324+
return peerData.pubKey
325+
}
318326

319-
if (peerData && peerData.id.pubKey) {
320-
log('getPublicKey: found local copy')
321-
return peerData.id.pubKey
327+
if (peerData.id.pubKey) {
328+
log('getPublicKey: found local copy')
329+
return peerData.id.pubKey
330+
}
331+
} catch (/** @type {any} */ err) {
332+
if (err.code !== 'ERR_NOT_FOUND') {
333+
throw err
334+
}
322335
}
323336

324337
// try the node directly

src/peer-routing/index.js

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,24 @@ class PeerRouting {
5252

5353
if (p) {
5454
this._log('findPeerLocal found %p in routing table', peer)
55-
peerData = await this._peerStore.get(p)
55+
56+
try {
57+
peerData = await this._peerStore.get(p)
58+
} catch (/** @type {any} */ err) {
59+
if (err.code !== 'ERR_NOT_FOUND') {
60+
throw err
61+
}
62+
}
5663
}
5764

5865
if (!peerData) {
59-
peerData = await this._peerStore.get(peer)
66+
try {
67+
peerData = await this._peerStore.get(peer)
68+
} catch (/** @type {any} */ err) {
69+
if (err.code !== 'ERR_NOT_FOUND') {
70+
throw err
71+
}
72+
}
6073
}
6174

6275
if (peerData) {
@@ -141,9 +154,9 @@ class PeerRouting {
141154
const match = peers.find((p) => p.equals(id))
142155

143156
if (match) {
144-
const peer = await this._peerStore.get(id)
157+
try {
158+
const peer = await this._peerStore.get(id)
145159

146-
if (peer) {
147160
this._log('found in peerStore')
148161
yield finalPeerEvent({
149162
from: this._peerId,
@@ -152,7 +165,12 @@ class PeerRouting {
152165
multiaddrs: peer.addresses.map((address) => address.multiaddr)
153166
}
154167
})
168+
155169
return
170+
} catch (/** @type {any} */ err) {
171+
if (err.code !== 'ERR_NOT_FOUND') {
172+
throw err
173+
}
156174
}
157175
}
158176

@@ -303,12 +321,18 @@ class PeerRouting {
303321
continue
304322
}
305323

306-
const peer = await this._peerStore.get(peerId)
324+
try {
325+
const addresses = await this._peerStore.addressBook.get(peerId)
307326

308-
output.push({
309-
id: peerId,
310-
multiaddrs: peer ? peer.addresses.map((address) => address.multiaddr) : []
311-
})
327+
output.push({
328+
id: peerId,
329+
multiaddrs: addresses.map((address) => address.multiaddr)
330+
})
331+
} catch (/** @type {any} */ err) {
332+
if (err.code !== 'ERR_NOT_FOUND') {
333+
throw err
334+
}
335+
}
312336
}
313337

314338
if (output.length) {

src/rpc/handlers/get-value.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ class GetValueHandler {
6060
if (this._peerId.equals(idFromKey)) {
6161
pubKey = this._peerId.pubKey
6262
} else {
63-
pubKey = await this._peerStore.keyBook.get(idFromKey)
63+
try {
64+
pubKey = await this._peerStore.keyBook.get(idFromKey)
65+
} catch (/** @type {any} */ err) {
66+
if (err.code !== 'ERR_NOT_FOUND') {
67+
throw err
68+
}
69+
}
6470
}
6571

6672
if (pubKey != null) {

0 commit comments

Comments
 (0)