diff --git a/package.json b/package.json index dd1ca351..55c6ea02 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/content-fetching/index.ts b/src/content-fetching/index.ts index e09fb908..dd144d5e 100644 --- a/src/content-fetching/index.ts +++ b/src/content-fetching/index.ts @@ -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' @@ -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') @@ -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') })) } } @@ -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) diff --git a/src/dual-kad-dht.ts b/src/dual-kad-dht.ts index 188f7d4e..69f3e66d 100644 --- a/src/dual-kad-dht.ts +++ b/src/dual-kad-dht.ts @@ -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' @@ -136,13 +136,13 @@ export class DualKadDHT extends EventEmitter 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') }) } } @@ -184,10 +184,10 @@ export class DualKadDHT extends EventEmitter 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') } } @@ -221,7 +221,7 @@ export class DualKadDHT extends EventEmitter implements Dua } if (!queriedPeers) { - throw errCode(new Error('Peer lookup failed'), 'ERR_LOOKUP_FAILED') + throw new CodeError('Peer lookup failed', 'ERR_LOOKUP_FAILED') } } diff --git a/src/network.ts b/src/network.ts index c88c6ae4..5652b29b 100644 --- a/src/network.ts +++ b/src/network.ts @@ -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' @@ -183,7 +183,7 @@ export class Network extends EventEmitter 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') } ) diff --git a/src/peer-routing/index.ts b/src/peer-routing/index.ts index bf271207..4b6eeba1 100644 --- a/src/peer-routing/index.ts +++ b/src/peer-routing/index.ts @@ -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' @@ -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') } /** @@ -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') }) } } @@ -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 } } @@ -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)) diff --git a/src/query/query-path.ts b/src/query/query-path.ts index 82c99669..c60148dc 100644 --- a/src/query/query-path.ts +++ b/src/query/query-path.ts @@ -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' @@ -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')) } }) diff --git a/src/rpc/handlers/add-provider.ts b/src/rpc/handlers/add-provider.ts index bb4f8503..6141a3c0 100644 --- a/src/rpc/handlers/add-provider.ts +++ b/src/rpc/handlers/add-provider.ts @@ -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' @@ -24,7 +24,7 @@ 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 @@ -32,7 +32,7 @@ export class AddProviderHandler implements DHTMessageHandler { // 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) { diff --git a/src/rpc/handlers/get-providers.ts b/src/rpc/handlers/get-providers.ts index 43e5d7a3..3cb9d4e0 100644 --- a/src/rpc/handlers/get-providers.ts +++ b/src/rpc/handlers/get-providers.ts @@ -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, @@ -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) diff --git a/src/rpc/handlers/get-value.ts b/src/rpc/handlers/get-value.ts index bb1425d6..6fced6e6 100644 --- a/src/rpc/handlers/get-value.ts +++ b/src/rpc/handlers/get-value.ts @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/src/rpc/handlers/put-value.ts b/src/rpc/handlers/put-value.ts index cfa3d172..0bceb71e 100644 --- a/src/rpc/handlers/put-value.ts +++ b/src/rpc/handlers/put-value.ts @@ -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' @@ -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 { diff --git a/test/kad-dht.spec.ts b/test/kad-dht.spec.ts index 13e89294..de5f096a 100644 --- a/test/kad-dht.spec.ts +++ b/test/kad-dht.spec.ts @@ -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' @@ -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') @@ -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(),