Skip to content

Commit bcacf5b

Browse files
committed
refactor: replace err-code with CodeError
Replaces [err-code](https://github.com/IndigoUnited/js-err-code/blob/master/index.js) with [CodeError](libp2p/js-libp2p-interfaces#314) Related: [js-libp2p#1269](libp2p/js-libp2p#1269) Changes - removes err-code from dependencies - adds @libp2p/interfaces@3.2.0 to dependencies - uses CodeError in place of err-code
1 parent d3ac6e6 commit bcacf5b

File tree

11 files changed

+36
-37
lines changed

11 files changed

+36
-37
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
"@libp2p/interface-peer-info": "^1.0.3",
154154
"@libp2p/interface-peer-store": "^1.2.2",
155155
"@libp2p/interface-registrar": "^2.0.3",
156-
"@libp2p/interfaces": "^3.0.3",
156+
"@libp2p/interfaces": "^3.2.0",
157157
"@libp2p/logger": "^2.0.1",
158158
"@libp2p/peer-collections": "^3.0.0",
159159
"@libp2p/peer-id": "^2.0.0",
@@ -163,7 +163,6 @@
163163
"abortable-iterator": "^4.0.2",
164164
"any-signal": "^3.0.0",
165165
"datastore-core": "^8.0.1",
166-
"err-code": "^3.0.1",
167166
"events": "^3.3.0",
168167
"hashlru": "^2.3.0",
169168
"interface-datastore": "^7.0.0",

src/content-fetching/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import errcode from 'err-code'
1+
import { CodeError } from '@libp2p/interfaces/errors'
22
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
33
import { Libp2pRecord } from '@libp2p/record'
44
import { verifyRecord } from '@libp2p/record/validators'
@@ -126,7 +126,7 @@ export class ContentFetching {
126126
}
127127

128128
if (!sentCorrection) {
129-
yield queryErrorEvent({ from, error: errcode(new Error('value not put correctly'), 'ERR_PUT_VALUE_INVALID') })
129+
yield queryErrorEvent({ from, error: new CodeError('value not put correctly', 'ERR_PUT_VALUE_INVALID') })
130130
}
131131

132132
this.log.error('Failed error correcting entry')
@@ -170,7 +170,7 @@ export class ContentFetching {
170170
}
171171

172172
if (!(putEvent.record != null && uint8ArrayEquals(putEvent.record.value, Libp2pRecord.deserialize(record).value))) {
173-
events.push(queryErrorEvent({ from: event.peer.id, error: errcode(new Error('value not put correctly'), 'ERR_PUT_VALUE_INVALID') }))
173+
events.push(queryErrorEvent({ from: event.peer.id, error: new CodeError('value not put correctly', 'ERR_PUT_VALUE_INVALID') }))
174174
}
175175
}
176176

@@ -225,7 +225,7 @@ export class ContentFetching {
225225
this.log('GetValue %b %b', key, best)
226226

227227
if (best == null) {
228-
throw errcode(new Error('best value was not found'), 'ERR_NOT_FOUND')
228+
throw new CodeError('best value was not found', 'ERR_NOT_FOUND')
229229
}
230230

231231
yield * this.sendCorrectionRecord(key, vals, best, options)

src/dual-kad-dht.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from '@libp2p/logger'
2-
import errCode from 'err-code'
2+
import { CodeError } from '@libp2p/interfaces/errors'
33
import merge from 'it-merge'
44
import { queryErrorEvent } from './query/events.js'
55
import type { KadDHT } from './kad-dht.js'
@@ -136,13 +136,13 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
136136
}
137137

138138
if (!queriedPeers) {
139-
throw errCode(new Error('No peers found in routing table!'), 'ERR_NO_PEERS_IN_ROUTING_TABLE')
139+
throw new CodeError('No peers found in routing table!', 'ERR_NO_PEERS_IN_ROUTING_TABLE')
140140
}
141141

142142
if (!foundValue) {
143143
yield queryErrorEvent({
144144
from: this.components.peerId,
145-
error: errCode(new Error('Not found'), 'ERR_NOT_FOUND')
145+
error: new CodeError('Not found', 'ERR_NOT_FOUND')
146146
})
147147
}
148148
}
@@ -184,10 +184,10 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
184184
if (success === 0) {
185185
if (errors.length > 0) {
186186
// if all sends failed, throw an error to inform the caller
187-
throw errCode(new Error(`Failed to provide to ${errors.length} of ${sent} peers`), 'ERR_PROVIDES_FAILED', { errors })
187+
throw new CodeError(`Failed to provide to ${errors.length} of ${sent} peers`, 'ERR_PROVIDES_FAILED', { errors })
188188
}
189189

190-
throw errCode(new Error('Failed to provide - no peers found'), 'ERR_PROVIDES_FAILED')
190+
throw new CodeError('Failed to provide - no peers found', 'ERR_PROVIDES_FAILED')
191191
}
192192
}
193193

@@ -221,7 +221,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
221221
}
222222

223223
if (!queriedPeers) {
224-
throw errCode(new Error('Peer lookup failed'), 'ERR_LOOKUP_FAILED')
224+
throw new CodeError('Peer lookup failed', 'ERR_LOOKUP_FAILED')
225225
}
226226
}
227227

src/network.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import errcode from 'err-code'
1+
import { CodeError } from '@libp2p/interfaces/errors'
22
import { pipe } from 'it-pipe'
33
import * as lp from 'it-length-prefixed'
44
import drain from 'it-drain'
@@ -183,7 +183,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
183183
return buf
184184
}
185185

186-
throw errcode(new Error('No message received'), 'ERR_NO_MESSAGE_RECEIVED')
186+
throw new CodeError('No message received', 'ERR_NO_MESSAGE_RECEIVED')
187187
}
188188
)
189189

src/peer-routing/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import errcode from 'err-code'
1+
import { CodeError } from '@libp2p/interfaces/errors'
22
import { verifyRecord } from '@libp2p/record/validators'
33
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
44
import { Message, MESSAGE_TYPE } from '../message/index.js'
@@ -116,18 +116,18 @@ export class PeerRouting {
116116

117117
// compare hashes of the pub key
118118
if (!recPeer.equals(peer)) {
119-
throw errcode(new Error('public key does not match id'), 'ERR_PUBLIC_KEY_DOES_NOT_MATCH_ID')
119+
throw new CodeError('public key does not match id', 'ERR_PUBLIC_KEY_DOES_NOT_MATCH_ID')
120120
}
121121

122122
if (recPeer.publicKey == null) {
123-
throw errcode(new Error('public key missing'), 'ERR_PUBLIC_KEY_MISSING')
123+
throw new CodeError('public key missing', 'ERR_PUBLIC_KEY_MISSING')
124124
}
125125

126126
yield valueEvent({ from: peer, value: recPeer.publicKey })
127127
}
128128
}
129129

130-
throw errcode(new Error(`Node not responding with its public key: ${peer.toString()}`), 'ERR_INVALID_RECORD')
130+
throw new CodeError(`Node not responding with its public key: ${peer.toString()}`, 'ERR_INVALID_RECORD')
131131
}
132132

133133
/**
@@ -207,7 +207,7 @@ export class PeerRouting {
207207
}
208208

209209
if (!foundPeer) {
210-
yield queryErrorEvent({ from: this.components.peerId, error: errcode(new Error('Not found'), 'ERR_NOT_FOUND') })
210+
yield queryErrorEvent({ from: this.components.peerId, error: new CodeError('Not found', 'ERR_NOT_FOUND') })
211211
}
212212
}
213213

@@ -270,7 +270,7 @@ export class PeerRouting {
270270
const errMsg = 'invalid record received, discarded'
271271
this.log(errMsg)
272272

273-
yield queryErrorEvent({ from: event.from, error: errcode(new Error(errMsg), 'ERR_INVALID_RECORD') })
273+
yield queryErrorEvent({ from: event.from, error: new CodeError(errMsg, 'ERR_INVALID_RECORD') })
274274
continue
275275
}
276276
}
@@ -286,7 +286,7 @@ export class PeerRouting {
286286
*/
287287
async _verifyRecordOnline (record: DHTRecord) {
288288
if (record.timeReceived == null) {
289-
throw errcode(new Error('invalid record received'), 'ERR_INVALID_RECORD')
289+
throw new CodeError('invalid record received', 'ERR_INVALID_RECORD')
290290
}
291291

292292
await verifyRecord(this.validators, new Libp2pRecord(record.key, record.value, record.timeReceived))

src/query/query-path.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Queue from 'p-queue'
22
import { xor } from 'uint8arrays/xor'
33
import { toString } from 'uint8arrays/to-string'
44
import defer from 'p-defer'
5-
import errCode from 'err-code'
5+
import { CodeError } from '@libp2p/interfaces/errors'
66
import { convertPeerId, convertBuffer } from '../utils.js'
77
import { TimeoutController } from 'timeout-abort-controller'
88
import { anySignal } from 'any-signal'
@@ -235,7 +235,7 @@ async function * toGenerator (queue: Queue, signal: AbortSignal, cleanUp: EventE
235235
cleanup()
236236

237237
if (wasRunning) {
238-
deferred.reject(errCode(new Error('Query aborted'), 'ERR_QUERY_ABORTED'))
238+
deferred.reject(new CodeError('Query aborted', 'ERR_QUERY_ABORTED'))
239239
}
240240
})
241241

src/rpc/handlers/add-provider.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CID } from 'multiformats/cid'
2-
import errcode from 'err-code'
2+
import { CodeError } from '@libp2p/interfaces/errors'
33
import { logger } from '@libp2p/logger'
44
import type { Providers } from '../../providers'
55
import type { PeerId } from '@libp2p/interface-peer-id'
@@ -24,15 +24,15 @@ export class AddProviderHandler implements DHTMessageHandler {
2424
log('start')
2525

2626
if (msg.key == null || msg.key.length === 0) {
27-
throw errcode(new Error('Missing key'), 'ERR_MISSING_KEY')
27+
throw new CodeError('Missing key', 'ERR_MISSING_KEY')
2828
}
2929

3030
let cid: CID
3131
try {
3232
// this is actually just the multihash, not the whole CID
3333
cid = CID.decode(msg.key)
3434
} catch (err: any) {
35-
throw errcode(new Error('Invalid CID'), 'ERR_INVALID_CID')
35+
throw new CodeError('Invalid CID', 'ERR_INVALID_CID')
3636
}
3737

3838
if (msg.providerPeers == null || msg.providerPeers.length === 0) {

src/rpc/handlers/get-providers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CID } from 'multiformats/cid'
2-
import errcode from 'err-code'
2+
import { CodeError } from '@libp2p/interfaces/errors'
33
import { Message } from '../../message/index.js'
44
import {
55
removePrivateAddresses,
@@ -45,7 +45,7 @@ export class GetProvidersHandler implements DHTMessageHandler {
4545
try {
4646
cid = CID.decode(msg.key)
4747
} catch (err: any) {
48-
throw errcode(new Error('Invalid CID'), 'ERR_INVALID_CID')
48+
throw new CodeError('Invalid CID', 'ERR_INVALID_CID')
4949
}
5050

5151
log('%p asking for providers for %s', peerId, cid)

src/rpc/handlers/get-value.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Libp2pRecord } from '@libp2p/record'
2-
import errcode from 'err-code'
2+
import { CodeError } from '@libp2p/interfaces/errors'
33
import { Message, MESSAGE_TYPE } from '../../message/index.js'
44
import {
55
MAX_RECORD_AGE
@@ -40,7 +40,7 @@ export class GetValueHandler implements DHTMessageHandler {
4040
log('%p asked for key %b', peerId, key)
4141

4242
if (key == null || key.length === 0) {
43-
throw errcode(new Error('Invalid key'), 'ERR_INVALID_KEY')
43+
throw new CodeError('Invalid key', 'ERR_INVALID_KEY')
4444
}
4545

4646
const response = new Message(MESSAGE_TYPE.GET_VALUE, key, msg.clusterLevel)
@@ -54,7 +54,7 @@ export class GetValueHandler implements DHTMessageHandler {
5454
const key = await this.components.peerStore.keyBook.get(idFromKey)
5555

5656
if (key == null) {
57-
throw errcode(new Error('No public key found in key book'), 'ERR_NOT_FOUND')
57+
throw new CodeError('No public key found in key book', 'ERR_NOT_FOUND')
5858
}
5959

6060
pubKey = key
@@ -114,7 +114,7 @@ export class GetValueHandler implements DHTMessageHandler {
114114
const record = Libp2pRecord.deserialize(rawRecord)
115115

116116
if (record == null) {
117-
throw errcode(new Error('Invalid record'), 'ERR_INVALID_RECORD')
117+
throw new CodeError('Invalid record', 'ERR_INVALID_RECORD')
118118
}
119119

120120
// Check validity: compare time received with max record age

src/rpc/handlers/put-value.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bufferToRecordKey } from '../../utils.js'
2-
import errcode from 'err-code'
2+
import { CodeError } from '@libp2p/interfaces/errors'
33
import { verifyRecord } from '@libp2p/record/validators'
44
import { Logger, logger } from '@libp2p/logger'
55
import type { DHTMessageHandler } from '../index.js'
@@ -39,7 +39,7 @@ export class PutValueHandler implements DHTMessageHandler {
3939
const errMsg = `Empty record from: ${peerId.toString()}`
4040

4141
this.log.error(errMsg)
42-
throw errcode(new Error(errMsg), 'ERR_EMPTY_RECORD')
42+
throw new CodeError(errMsg, 'ERR_EMPTY_RECORD')
4343
}
4444

4545
try {

test/kad-dht.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { expect } from 'aegir/chai'
55
import sinon from 'sinon'
66
import { Libp2pRecord } from '@libp2p/record'
7-
import errcode from 'err-code'
7+
import { CodeError } from '@libp2p/interfaces/errors'
88
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
99
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1010
import drain from 'it-drain'
@@ -213,7 +213,7 @@ describe('KadDHT', () => {
213213
this.timeout(10 * 1000)
214214

215215
const errCode = 'ERR_NOT_AVAILABLE'
216-
const error = errcode(new Error('fake error'), errCode)
216+
const error = new CodeError('fake error', errCode)
217217
const key = uint8ArrayFromString('/v/hello')
218218
const value = uint8ArrayFromString('world')
219219

@@ -797,7 +797,7 @@ describe('KadDHT', () => {
797797
this.timeout(240 * 1000)
798798

799799
const errCode = 'ERR_INVALID_RECORD_FAKE'
800-
const error = errcode(new Error('fake error'), errCode)
800+
const error = new CodeError('fake error', errCode)
801801

802802
const [dhtA, dhtB] = await Promise.all([
803803
tdht.spawn(),

0 commit comments

Comments
 (0)