Skip to content

Commit

Permalink
refactor: 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#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
  • Loading branch information
tabcat committed Jan 9, 2023
1 parent 12e3942 commit 843a672
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/interface-mocks/src/connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Connection } from '@libp2p/interface-connection'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interface-connection-manager'
import { connectionPair } from './connection.js'
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import type { Registrar } from '@libp2p/interface-registrar'
import type { PubSub } from '@libp2p/interface-pubsub'

Expand All @@ -29,7 +29,7 @@ class MockNetwork {
}
}

throw errCode(new Error('Peer not found'), 'ERR_PEER_NOT_FOUND')
throw new CodeError('Peer not found', 'ERR_PEER_NOT_FOUND')
}

reset () {
Expand Down Expand Up @@ -77,7 +77,7 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem

async openConnection (peerId: PeerId) {
if (this.components == null) {
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED')
}

const existingConnections = this.getConnections(peerId)
Expand Down Expand Up @@ -119,7 +119,7 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem

async closeConnections (peerId: PeerId) {
if (this.components == null) {
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED')
}

const connections = this.getConnections(peerId)
Expand Down
4 changes: 2 additions & 2 deletions packages/interface-mocks/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as STATUS from '@libp2p/interface-connection/status'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
import type { AbortOptions } from '@libp2p/interfaces'
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import type { Uint8ArrayList } from 'uint8arraylist'

const log = logger('libp2p:mock-connection')
Expand Down Expand Up @@ -75,7 +75,7 @@ class MockConnection implements Connection {
}

if (this.stat.status !== STATUS.OPEN) {
throw errCode(new Error('connection must be open to create streams'), 'ERR_CONNECTION_CLOSED')
throw new CodeError('connection must be open to create streams', 'ERR_CONNECTION_CLOSED')
}

const id = `${Math.random()}`
Expand Down
6 changes: 3 additions & 3 deletions packages/interface-mocks/src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { abortableSource } from 'abortable-iterator'
import { anySignal } from 'any-signal'
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import { Logger, logger } from '@libp2p/logger'
import * as ndjson from 'it-ndjson'
import type { Stream } from '@libp2p/interface-connection'
Expand Down Expand Up @@ -125,7 +125,7 @@ class MuxedStream {
id,
sink: async (source) => {
if (this.sinkEnded) {
throw errCode(new Error('stream closed for writing'), 'ERR_SINK_ENDED')
throw new CodeError('stream closed for writing', 'ERR_SINK_ENDED')
}

source = abortableSource(source, anySignal([
Expand Down Expand Up @@ -243,7 +243,7 @@ class MuxedStream {

// Close immediately for reading and writing (remote error)
reset: () => {
const err = errCode(new Error('stream reset'), 'ERR_STREAM_RESET')
const err = new CodeError('stream reset', 'ERR_STREAM_RESET')
this.resetController.abort()
this.input.end(err)
onSinkEnd(err)
Expand Down

0 comments on commit 843a672

Please sign in to comment.