Skip to content

Commit

Permalink
chore: replace err-code with CodeError
Browse files Browse the repository at this point in the history
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/[email protected] to dependencies
- uses CodeError in place of err-code
  • Loading branch information
tabcat committed Jan 10, 2023
1 parent d3ac6e6 commit 9124f98
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 37 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"@libp2p/interface-peer-info": "^1.0.3",
"@libp2p/interface-peer-store": "^1.2.2",
"@libp2p/interface-registrar": "^2.0.3",
"@libp2p/interfaces": "^3.0.3",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.1",
"@libp2p/peer-collections": "^3.0.0",
"@libp2p/peer-id": "^2.0.0",
Expand All @@ -163,7 +163,6 @@
"abortable-iterator": "^4.0.2",
"any-signal": "^3.0.0",
"datastore-core": "^8.0.1",
"err-code": "^3.0.1",
"events": "^3.3.0",
"hashlru": "^2.3.0",
"interface-datastore": "^7.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/content-fetching/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { Libp2pRecord } from '@libp2p/record'
import { verifyRecord } from '@libp2p/record/validators'
Expand Down Expand Up @@ -126,7 +126,7 @@ export class ContentFetching {
}

if (!sentCorrection) {
yield queryErrorEvent({ from, error: errcode(new Error('value not put correctly'), 'ERR_PUT_VALUE_INVALID') })
yield queryErrorEvent({ from, error: new CodeError('value not put correctly', 'ERR_PUT_VALUE_INVALID') })
}

this.log.error('Failed error correcting entry')
Expand Down Expand Up @@ -170,7 +170,7 @@ export class ContentFetching {
}

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

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

if (best == null) {
throw errcode(new Error('best value was not found'), 'ERR_NOT_FOUND')
throw new CodeError('best value was not found', 'ERR_NOT_FOUND')
}

yield * this.sendCorrectionRecord(key, vals, best, options)
Expand Down
12 changes: 6 additions & 6 deletions src/dual-kad-dht.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logger } from '@libp2p/logger'
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import merge from 'it-merge'
import { queryErrorEvent } from './query/events.js'
import type { KadDHT } from './kad-dht.js'
Expand Down Expand Up @@ -136,13 +136,13 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
}

if (!queriedPeers) {
throw errCode(new Error('No peers found in routing table!'), 'ERR_NO_PEERS_IN_ROUTING_TABLE')
throw new CodeError('No peers found in routing table!', 'ERR_NO_PEERS_IN_ROUTING_TABLE')
}

if (!foundValue) {
yield queryErrorEvent({
from: this.components.peerId,
error: errCode(new Error('Not found'), 'ERR_NOT_FOUND')
error: new CodeError('Not found', 'ERR_NOT_FOUND')
})
}
}
Expand Down Expand Up @@ -184,10 +184,10 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
if (success === 0) {
if (errors.length > 0) {
// if all sends failed, throw an error to inform the caller
throw errCode(new Error(`Failed to provide to ${errors.length} of ${sent} peers`), 'ERR_PROVIDES_FAILED', { errors })
throw new CodeError(`Failed to provide to ${errors.length} of ${sent} peers`, 'ERR_PROVIDES_FAILED', { errors })
}

throw errCode(new Error('Failed to provide - no peers found'), 'ERR_PROVIDES_FAILED')
throw new CodeError('Failed to provide - no peers found', 'ERR_PROVIDES_FAILED')
}
}

Expand Down Expand Up @@ -221,7 +221,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
}

if (!queriedPeers) {
throw errCode(new Error('Peer lookup failed'), 'ERR_LOOKUP_FAILED')
throw new CodeError('Peer lookup failed', 'ERR_LOOKUP_FAILED')
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { pipe } from 'it-pipe'
import * as lp from 'it-length-prefixed'
import drain from 'it-drain'
Expand Down Expand Up @@ -183,7 +183,7 @@ export class Network extends EventEmitter<NetworkEvents> implements Startable {
return buf
}

throw errcode(new Error('No message received'), 'ERR_NO_MESSAGE_RECEIVED')
throw new CodeError('No message received', 'ERR_NO_MESSAGE_RECEIVED')
}
)

Expand Down
14 changes: 7 additions & 7 deletions src/peer-routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { verifyRecord } from '@libp2p/record/validators'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { Message, MESSAGE_TYPE } from '../message/index.js'
Expand Down Expand Up @@ -116,18 +116,18 @@ export class PeerRouting {

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

if (recPeer.publicKey == null) {
throw errcode(new Error('public key missing'), 'ERR_PUBLIC_KEY_MISSING')
throw new CodeError('public key missing', 'ERR_PUBLIC_KEY_MISSING')
}

yield valueEvent({ from: peer, value: recPeer.publicKey })
}
}

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

/**
Expand Down Expand Up @@ -207,7 +207,7 @@ export class PeerRouting {
}

if (!foundPeer) {
yield queryErrorEvent({ from: this.components.peerId, error: errcode(new Error('Not found'), 'ERR_NOT_FOUND') })
yield queryErrorEvent({ from: this.components.peerId, error: new CodeError('Not found', 'ERR_NOT_FOUND') })
}
}

Expand Down Expand Up @@ -270,7 +270,7 @@ export class PeerRouting {
const errMsg = 'invalid record received, discarded'
this.log(errMsg)

yield queryErrorEvent({ from: event.from, error: errcode(new Error(errMsg), 'ERR_INVALID_RECORD') })
yield queryErrorEvent({ from: event.from, error: new CodeError(errMsg, 'ERR_INVALID_RECORD') })
continue
}
}
Expand All @@ -286,7 +286,7 @@ export class PeerRouting {
*/
async _verifyRecordOnline (record: DHTRecord) {
if (record.timeReceived == null) {
throw errcode(new Error('invalid record received'), 'ERR_INVALID_RECORD')
throw new CodeError('invalid record received', 'ERR_INVALID_RECORD')
}

await verifyRecord(this.validators, new Libp2pRecord(record.key, record.value, record.timeReceived))
Expand Down
4 changes: 2 additions & 2 deletions src/query/query-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Queue from 'p-queue'
import { xor } from 'uint8arrays/xor'
import { toString } from 'uint8arrays/to-string'
import defer from 'p-defer'
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { convertPeerId, convertBuffer } from '../utils.js'
import { TimeoutController } from 'timeout-abort-controller'
import { anySignal } from 'any-signal'
Expand Down Expand Up @@ -235,7 +235,7 @@ async function * toGenerator (queue: Queue, signal: AbortSignal, cleanUp: EventE
cleanup()

if (wasRunning) {
deferred.reject(errCode(new Error('Query aborted'), 'ERR_QUERY_ABORTED'))
deferred.reject(new CodeError('Query aborted', 'ERR_QUERY_ABORTED'))
}
})

Expand Down
6 changes: 3 additions & 3 deletions src/rpc/handlers/add-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CID } from 'multiformats/cid'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { logger } from '@libp2p/logger'
import type { Providers } from '../../providers'
import type { PeerId } from '@libp2p/interface-peer-id'
Expand All @@ -24,15 +24,15 @@ export class AddProviderHandler implements DHTMessageHandler {
log('start')

if (msg.key == null || msg.key.length === 0) {
throw errcode(new Error('Missing key'), 'ERR_MISSING_KEY')
throw new CodeError('Missing key', 'ERR_MISSING_KEY')
}

let cid: CID
try {
// this is actually just the multihash, not the whole CID
cid = CID.decode(msg.key)
} catch (err: any) {
throw errcode(new Error('Invalid CID'), 'ERR_INVALID_CID')
throw new CodeError('Invalid CID', 'ERR_INVALID_CID')
}

if (msg.providerPeers == null || msg.providerPeers.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/handlers/get-providers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CID } from 'multiformats/cid'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { Message } from '../../message/index.js'
import {
removePrivateAddresses,
Expand Down Expand Up @@ -45,7 +45,7 @@ export class GetProvidersHandler implements DHTMessageHandler {
try {
cid = CID.decode(msg.key)
} catch (err: any) {
throw errcode(new Error('Invalid CID'), 'ERR_INVALID_CID')
throw new CodeError('Invalid CID', 'ERR_INVALID_CID')
}

log('%p asking for providers for %s', peerId, cid)
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/handlers/get-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Libp2pRecord } from '@libp2p/record'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { Message, MESSAGE_TYPE } from '../../message/index.js'
import {
MAX_RECORD_AGE
Expand Down Expand Up @@ -40,7 +40,7 @@ export class GetValueHandler implements DHTMessageHandler {
log('%p asked for key %b', peerId, key)

if (key == null || key.length === 0) {
throw errcode(new Error('Invalid key'), 'ERR_INVALID_KEY')
throw new CodeError('Invalid key', 'ERR_INVALID_KEY')
}

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

if (key == null) {
throw errcode(new Error('No public key found in key book'), 'ERR_NOT_FOUND')
throw new CodeError('No public key found in key book', 'ERR_NOT_FOUND')
}

pubKey = key
Expand Down Expand Up @@ -114,7 +114,7 @@ export class GetValueHandler implements DHTMessageHandler {
const record = Libp2pRecord.deserialize(rawRecord)

if (record == null) {
throw errcode(new Error('Invalid record'), 'ERR_INVALID_RECORD')
throw new CodeError('Invalid record', 'ERR_INVALID_RECORD')
}

// Check validity: compare time received with max record age
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/handlers/put-value.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bufferToRecordKey } from '../../utils.js'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { verifyRecord } from '@libp2p/record/validators'
import { Logger, logger } from '@libp2p/logger'
import type { DHTMessageHandler } from '../index.js'
Expand Down Expand Up @@ -39,7 +39,7 @@ export class PutValueHandler implements DHTMessageHandler {
const errMsg = `Empty record from: ${peerId.toString()}`

this.log.error(errMsg)
throw errcode(new Error(errMsg), 'ERR_EMPTY_RECORD')
throw new CodeError(errMsg, 'ERR_EMPTY_RECORD')
}

try {
Expand Down
6 changes: 3 additions & 3 deletions test/kad-dht.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { expect } from 'aegir/chai'
import sinon from 'sinon'
import { Libp2pRecord } from '@libp2p/record'
import errcode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import drain from 'it-drain'
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('KadDHT', () => {
this.timeout(10 * 1000)

const errCode = 'ERR_NOT_AVAILABLE'
const error = errcode(new Error('fake error'), errCode)
const error = new CodeError('fake error', errCode)
const key = uint8ArrayFromString('/v/hello')
const value = uint8ArrayFromString('world')

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

const errCode = 'ERR_INVALID_RECORD_FAKE'
const error = errcode(new Error('fake error'), errCode)
const error = new CodeError('fake error', errCode)

const [dhtA, dhtB] = await Promise.all([
tdht.spawn(),
Expand Down

0 comments on commit 9124f98

Please sign in to comment.