Skip to content

Commit aad13d0

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/js-libp2p-interfaces#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
1 parent 4f0939f commit aad13d0

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@
143143
"docs": "aegir docs"
144144
},
145145
"dependencies": {
146-
"@libp2p/interfaces": "^3.0.2",
146+
"@libp2p/interfaces": "^3.2.0",
147147
"@libp2p/logger": "^2.0.0",
148148
"abortable-iterator": "^4.0.2",
149-
"err-code": "^3.0.1",
150149
"it-first": "^2.0.0",
151150
"it-handshake": "^4.1.2",
152151
"it-length-prefixed": "^8.0.3",

src/multistream.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Uint8ArrayList } from 'uint8arraylist'
33
import * as lp from 'it-length-prefixed'
44
import { pipe } from 'it-pipe'
5-
import errCode from 'err-code'
5+
import { CodeError } from '@libp2p/interfaces/errors'
66
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
77
import first from 'it-first'
88
import { abortableSource } from 'abortable-iterator'
@@ -82,12 +82,12 @@ export async function read (reader: Reader, options?: AbortOptions): Promise<Uin
8282
)
8383

8484
if (buf == null || buf.length === 0) {
85-
throw errCode(new Error('no buffer returned'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
85+
throw new CodeError('no buffer returned', 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
8686
}
8787

8888
if (buf.get(buf.byteLength - 1) !== NewLine[0]) {
8989
log.error('Invalid mss message - missing newline - %s', buf.subarray())
90-
throw errCode(new Error('missing newline'), 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
90+
throw new CodeError('missing newline', 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
9191
}
9292

9393
return buf.sublist(0, -1) // Remove newline

src/select.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from '@libp2p/logger'
2-
import errCode from 'err-code'
2+
import { CodeError } from '@libp2p/interfaces/errors'
33
import * as multistream from './multistream.js'
44
import { handshake } from 'it-handshake'
55
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
@@ -102,7 +102,7 @@ export async function select (stream: Duplex<any>, protocols: string | string[],
102102
}
103103

104104
rest()
105-
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL')
105+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL')
106106
}
107107

108108
/**
@@ -147,7 +147,7 @@ export function lazySelect (stream: Duplex<any>, protocol: string): ProtocolStre
147147
response = await multistream.readString(byteReader)
148148
}
149149
if (response !== protocol) {
150-
throw errCode(new Error('protocol selection failed'), 'ERR_UNSUPPORTED_PROTOCOL')
150+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL')
151151
}
152152
for await (const chunk of byteReader) {
153153
yield * chunk

0 commit comments

Comments
 (0)