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

Commit

Permalink
chore: replace err-code with CodeError (#53)
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 authored Jan 13, 2023
1 parent 7ea06a8 commit e6b87d7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/address-book.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 { isMultiaddr } from '@multiformats/multiaddr'
import { codes } from './errors.js'
import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)),
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -125,7 +125,7 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> 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')
Expand All @@ -137,7 +137,7 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> 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')
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/key-book.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 { codes } from './errors.js'
import { peerIdFromPeerId } from '@libp2p/peer-id'
import { equals as uint8arrayEquals } from 'uint8arrays/equals'
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions src/metadata-book.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 { codes } from './errors.js'
import { peerIdFromPeerId } from '@libp2p/peer-id'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
8 changes: 4 additions & 4 deletions src/proto-book.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 { codes } from './errors.js'
import { peerIdFromPeerId } from '@libp2p/peer-id'
import { CustomEvent } from '@libp2p/interfaces/events'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 3 additions & 3 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e6b87d7

Please sign in to comment.