Skip to content

Commit 843a672

Browse files
committed
refactor: replace err-code with CodeError
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/[email protected] to dependencies - uses CodeError in place of err-code
1 parent 12e3942 commit 843a672

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

packages/interface-mocks/src/connection-manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Connection } from '@libp2p/interface-connection'
44
import type { PeerId } from '@libp2p/interface-peer-id'
55
import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interface-connection-manager'
66
import { connectionPair } from './connection.js'
7-
import errCode from 'err-code'
7+
import { CodeError } from '@libp2p/interfaces/errors'
88
import type { Registrar } from '@libp2p/interface-registrar'
99
import type { PubSub } from '@libp2p/interface-pubsub'
1010

@@ -29,7 +29,7 @@ class MockNetwork {
2929
}
3030
}
3131

32-
throw errCode(new Error('Peer not found'), 'ERR_PEER_NOT_FOUND')
32+
throw new CodeError('Peer not found', 'ERR_PEER_NOT_FOUND')
3333
}
3434

3535
reset () {
@@ -77,7 +77,7 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem
7777

7878
async openConnection (peerId: PeerId) {
7979
if (this.components == null) {
80-
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
80+
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED')
8181
}
8282

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

120120
async closeConnections (peerId: PeerId) {
121121
if (this.components == null) {
122-
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
122+
throw new CodeError('Not initialized', 'ERR_NOT_INITIALIZED')
123123
}
124124

125125
const connections = this.getConnections(peerId)

packages/interface-mocks/src/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as STATUS from '@libp2p/interface-connection/status'
1414
import type { Multiaddr } from '@multiformats/multiaddr'
1515
import type { StreamMuxer, StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
1616
import type { AbortOptions } from '@libp2p/interfaces'
17-
import errCode from 'err-code'
17+
import { CodeError } from '@libp2p/interfaces/errors'
1818
import type { Uint8ArrayList } from 'uint8arraylist'
1919

2020
const log = logger('libp2p:mock-connection')
@@ -75,7 +75,7 @@ class MockConnection implements Connection {
7575
}
7676

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

8181
const id = `${Math.random()}`

packages/interface-mocks/src/muxer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
33
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
44
import { abortableSource } from 'abortable-iterator'
55
import { anySignal } from 'any-signal'
6-
import errCode from 'err-code'
6+
import { CodeError } from '@libp2p/interfaces/errors'
77
import { Logger, logger } from '@libp2p/logger'
88
import * as ndjson from 'it-ndjson'
99
import type { Stream } from '@libp2p/interface-connection'
@@ -125,7 +125,7 @@ class MuxedStream {
125125
id,
126126
sink: async (source) => {
127127
if (this.sinkEnded) {
128-
throw errCode(new Error('stream closed for writing'), 'ERR_SINK_ENDED')
128+
throw new CodeError('stream closed for writing', 'ERR_SINK_ENDED')
129129
}
130130

131131
source = abortableSource(source, anySignal([
@@ -243,7 +243,7 @@ class MuxedStream {
243243

244244
// Close immediately for reading and writing (remote error)
245245
reset: () => {
246-
const err = errCode(new Error('stream reset'), 'ERR_STREAM_RESET')
246+
const err = new CodeError('stream reset', 'ERR_STREAM_RESET')
247247
this.resetController.abort()
248248
this.input.end(err)
249249
onSinkEnd(err)

0 commit comments

Comments
 (0)