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

Commit

Permalink
fix: replace err-code with CodeError (#334)
Browse files Browse the repository at this point in the history
* chore: replace err-code with CodeError

Replaces [err-code](https://github.com/IndigoUnited/js-err-code/blob/master/index.js) with [CodeError](#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

* remove err-code dep

---------

Co-authored-by: Marin Petrunic <marin.petrunic@gmail.com>
  • Loading branch information
tabcat and mpetrunic authored Feb 22, 2023
1 parent fc1f2cb commit a909d41
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
1 change: 0 additions & 1 deletion packages/interface-mocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@
"@multiformats/multiaddr": "^11.0.0",
"abortable-iterator": "^4.0.2",
"any-signal": "^3.0.1",
"err-code": "^3.0.1",
"it-handshake": "^4.0.0",
"it-map": "^2.0.0",
"it-ndjson": "^1.0.0",
Expand Down
10 changes: 5 additions & 5 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'
import { isMultiaddr, Multiaddr } from '@multiformats/multiaddr'
Expand All @@ -30,7 +30,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 (): void {
Expand Down Expand Up @@ -78,11 +78,11 @@ class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implem

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

if (isMultiaddr(peerId)) {
throw errCode(new Error('Dialing multiaddrs not supported'), 'ERR_NOT_SUPPORTED')
throw new CodeError('Dialing multiaddrs not supported', 'ERR_NOT_SUPPORTED')
}

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

async closeConnections (peerId: PeerId): Promise<void> {
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 a909d41

Please sign in to comment.