diff --git a/package.json b/package.json index 2733b15..bb44a77 100644 --- a/package.json +++ b/package.json @@ -148,12 +148,11 @@ "@libp2p/interface-peer-info": "^1.0.3", "@libp2p/interface-peer-store": "^1.2.2", "@libp2p/interface-record": "^2.0.1", - "@libp2p/interfaces": "^3.0.3", + "@libp2p/interfaces": "^3.2.0", "@libp2p/logger": "^2.0.0", "@libp2p/peer-id": "^2.0.0", "@libp2p/peer-record": "^5.0.0", "@multiformats/multiaddr": "^11.0.0", - "err-code": "^3.0.1", "interface-datastore": "^7.0.0", "it-all": "^2.0.0", "it-filter": "^2.0.0", diff --git a/src/address-book.ts b/src/address-book.ts index 265a928..eaf8760 100644 --- a/src/address-book.ts +++ b/src/address-book.ts @@ -1,5 +1,5 @@ import { logger } from '@libp2p/logger' -import errcode from 'err-code' +import { CodeError } from '@libp2p/interfaces/errors' import { isMultiaddr } from '@multiformats/multiaddr' import { codes } from './errors.js' import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record' @@ -174,7 +174,7 @@ export class PeerStoreAddressBook { if (!Array.isArray(multiaddrs)) { log.error('multiaddrs must be an array of Multiaddrs') - throw errcode(new Error('multiaddrs must be an array of Multiaddrs'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('multiaddrs must be an array of Multiaddrs', codes.ERR_INVALID_PARAMETERS) } log.trace('set await write lock') @@ -244,7 +244,7 @@ export class PeerStoreAddressBook { if (!Array.isArray(multiaddrs)) { log.error('multiaddrs must be an array of Multiaddrs') - throw errcode(new Error('multiaddrs must be an array of Multiaddrs'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('multiaddrs must be an array of Multiaddrs', codes.ERR_INVALID_PARAMETERS) } log.trace('add await write lock') @@ -351,7 +351,7 @@ async function filterMultiaddrs (peerId: PeerId, multiaddrs: Multiaddr[], addres (source) => each(source, (multiaddr) => { if (!isMultiaddr(multiaddr)) { log.error('multiaddr must be an instance of Multiaddr') - throw errcode(new Error('multiaddr must be an instance of Multiaddr'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('multiaddr must be an instance of Multiaddr', codes.ERR_INVALID_PARAMETERS) } }), (source) => filter(source, async (multiaddr) => await addressFilter(peerId, multiaddr)), diff --git a/src/index.ts b/src/index.ts index f9e09c0..8932fbe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { PeerStoreProtoBook } from './proto-book.js' import { PersistentStore, Store } from './store.js' import type { PeerStore, AddressBook, KeyBook, MetadataBook, ProtoBook, PeerStoreEvents, PeerStoreInit, Peer, TagOptions } from '@libp2p/interface-peer-store' import type { PeerId } from '@libp2p/interface-peer-id' -import errCode from 'err-code' +import { CodeError } from '@libp2p/interfaces/errors' import { Tag, Tags } from './pb/tags.js' import type { Datastore } from 'interface-datastore' @@ -125,7 +125,7 @@ export class PersistentPeerStore extends EventEmitter implement const ttl = options.ttl ?? undefined if (value !== providedValue || value < 0 || value > 100) { - throw errCode(new Error('Tag value must be between 0-100'), 'ERR_TAG_VALUE_OUT_OF_BOUNDS') + throw new CodeError('Tag value must be between 0-100', 'ERR_TAG_VALUE_OUT_OF_BOUNDS') } const buf = await this.metadataBook.getValue(peerId, 'tags') @@ -137,7 +137,7 @@ export class PersistentPeerStore extends EventEmitter implement for (const t of tags) { if (t.name === tag) { - throw errCode(new Error('Peer already tagged'), 'ERR_DUPLICATE_TAG') + throw new CodeError('Peer already tagged', 'ERR_DUPLICATE_TAG') } } diff --git a/src/key-book.ts b/src/key-book.ts index ff06028..65ef260 100644 --- a/src/key-book.ts +++ b/src/key-book.ts @@ -1,5 +1,5 @@ import { logger } from '@libp2p/logger' -import errcode from 'err-code' +import { CodeError } from '@libp2p/interfaces/errors' import { codes } from './errors.js' import { peerIdFromPeerId } from '@libp2p/peer-id' import { equals as uint8arrayEquals } from 'uint8arrays/equals' @@ -32,7 +32,7 @@ export class PeerStoreKeyBook implements KeyBook { if (!(publicKey instanceof Uint8Array)) { log.error('publicKey must be an instance of Uint8Array to store data') - throw errcode(new Error('publicKey must be an instance of PublicKey'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('publicKey must be an instance of PublicKey', codes.ERR_INVALID_PARAMETERS) } log.trace('set await write lock') diff --git a/src/metadata-book.ts b/src/metadata-book.ts index 84fc535..f60fd4e 100644 --- a/src/metadata-book.ts +++ b/src/metadata-book.ts @@ -1,5 +1,5 @@ import { logger } from '@libp2p/logger' -import errcode from 'err-code' +import { CodeError } from '@libp2p/interfaces/errors' import { codes } from './errors.js' import { peerIdFromPeerId } from '@libp2p/peer-id' import { equals as uint8ArrayEquals } from 'uint8arrays/equals' @@ -80,7 +80,7 @@ export class PeerStoreMetadataBook implements MetadataBook { if (!(metadata instanceof Map)) { log.error('valid metadata must be provided to store data') - throw errcode(new Error('valid metadata must be provided'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('valid metadata must be provided', codes.ERR_INVALID_PARAMETERS) } log.trace('set await write lock') @@ -123,7 +123,7 @@ export class PeerStoreMetadataBook implements MetadataBook { if (typeof key !== 'string' || !(value instanceof Uint8Array)) { log.error('valid key and value must be provided to store data') - throw errcode(new Error('valid key and value must be provided'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('valid key and value must be provided', codes.ERR_INVALID_PARAMETERS) } log.trace('setValue await write lock') diff --git a/src/proto-book.ts b/src/proto-book.ts index 067758c..3fb0b7a 100644 --- a/src/proto-book.ts +++ b/src/proto-book.ts @@ -1,5 +1,5 @@ import { logger } from '@libp2p/logger' -import errcode from 'err-code' +import { CodeError } from '@libp2p/interfaces/errors' import { codes } from './errors.js' import { peerIdFromPeerId } from '@libp2p/peer-id' import { CustomEvent } from '@libp2p/interfaces/events' @@ -50,7 +50,7 @@ export class PeerStoreProtoBook implements ProtoBook { if (!Array.isArray(protocols)) { log.error('protocols must be provided to store data') - throw errcode(new Error('protocols must be provided'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS) } log.trace('set await write lock') @@ -99,7 +99,7 @@ export class PeerStoreProtoBook implements ProtoBook { if (!Array.isArray(protocols)) { log.error('protocols must be provided to store data') - throw errcode(new Error('protocols must be provided'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS) } log.trace('add await write lock') @@ -149,7 +149,7 @@ export class PeerStoreProtoBook implements ProtoBook { if (!Array.isArray(protocols)) { log.error('protocols must be provided to store data') - throw errcode(new Error('protocols must be provided'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('protocols must be provided', codes.ERR_INVALID_PARAMETERS) } log.trace('remove await write lock') diff --git a/src/store.ts b/src/store.ts index 46af992..ea0c6dd 100644 --- a/src/store.ts +++ b/src/store.ts @@ -1,6 +1,6 @@ import { logger } from '@libp2p/logger' import { peerIdFromBytes } from '@libp2p/peer-id' -import errcode from 'err-code' +import { CodeError } from '@libp2p/interfaces/errors' import { codes } from './errors.js' import { Key } from 'interface-datastore/key' import { base32 } from 'multiformats/bases/base32' @@ -48,7 +48,7 @@ export class PersistentStore { _peerIdToDatastoreKey (peerId: PeerId) { if (peerId.type == null) { log.error('peerId must be an instance of peer-id to store data') - throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('peerId must be an instance of peer-id', codes.ERR_INVALID_PARAMETERS) } const b32key = peerId.toCID().toString() @@ -90,7 +90,7 @@ export class PersistentStore { async save (peer: Peer) { if (peer.pubKey != null && peer.id.publicKey != null && !uint8arrayEquals(peer.pubKey, peer.id.publicKey)) { log.error('peer publicKey bytes do not match peer id publicKey bytes') - throw errcode(new Error('publicKey bytes do not match peer id publicKey bytes'), codes.ERR_INVALID_PARAMETERS) + throw new CodeError('publicKey bytes do not match peer id publicKey bytes', codes.ERR_INVALID_PARAMETERS) } // dedupe addresses