Skip to content

Commit e121e4b

Browse files
authored
chore: replace err-code with CodeError (#116)
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](#1269) Changes - removes err-code from dependencies - adds @libp2p/interfaces@3.2.0 to dependencies - uses CodeError in place of err-code
1 parent 8f85f86 commit e121e4b

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,13 @@
182182
"@libp2p/interface-peer-id": "^2.0.0",
183183
"@libp2p/interface-pubsub": "^3.0.0",
184184
"@libp2p/interface-registrar": "^2.0.0",
185-
"@libp2p/interfaces": "^3.0.2",
185+
"@libp2p/interfaces": "^3.2.0",
186186
"@libp2p/logger": "^2.0.0",
187187
"@libp2p/peer-collections": "^3.0.0",
188188
"@libp2p/peer-id": "^2.0.0",
189189
"@libp2p/topology": "^4.0.0",
190190
"@multiformats/multiaddr": "^11.0.0",
191191
"abortable-iterator": "^4.0.2",
192-
"err-code": "^3.0.1",
193192
"it-length-prefixed": "^8.0.2",
194193
"it-pipe": "^2.0.3",
195194
"it-pushable": "^3.0.0",

src/index.ts

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from '@libp2p/logger'
22
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
3-
import errcode from 'err-code'
3+
import { CodeError } from '@libp2p/interfaces/errors'
44
import { pipe } from 'it-pipe'
55
import Queue from 'p-queue'
66
import { createTopology } from '@libp2p/topology'
@@ -447,22 +447,22 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
447447
switch (signaturePolicy) {
448448
case 'StrictSign':
449449
if (msg.type !== 'signed') {
450-
throw errcode(new Error('Message type should be "signed" when signature policy is StrictSign but it was not'), codes.ERR_MISSING_SIGNATURE)
450+
throw new CodeError('Message type should be "signed" when signature policy is StrictSign but it was not', codes.ERR_MISSING_SIGNATURE)
451451
}
452452

453453
if (msg.sequenceNumber == null) {
454-
throw errcode(new Error('Need seqno when signature policy is StrictSign but it was missing'), codes.ERR_MISSING_SEQNO)
454+
throw new CodeError('Need seqno when signature policy is StrictSign but it was missing', codes.ERR_MISSING_SEQNO)
455455
}
456456

457457
if (msg.key == null) {
458-
throw errcode(new Error('Need key when signature policy is StrictSign but it was missing'), codes.ERR_MISSING_KEY)
458+
throw new CodeError('Need key when signature policy is StrictSign but it was missing', codes.ERR_MISSING_KEY)
459459
}
460460

461461
return msgId(msg.key, msg.sequenceNumber)
462462
case 'StrictNoSign':
463463
return noSignMsgId(msg.data)
464464
default:
465-
throw errcode(new Error('Cannot get message id: unhandled signature policy'), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
465+
throw new CodeError('Cannot get message id: unhandled signature policy', codes.ERR_UNHANDLED_SIGNATURE_POLICY)
466466
}
467467
}
468468

@@ -528,51 +528,51 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
528528
switch (signaturePolicy) {
529529
case 'StrictNoSign':
530530
if (message.type !== 'unsigned') {
531-
throw errcode(new Error('Message type should be "unsigned" when signature policy is StrictNoSign but it was not'), codes.ERR_MISSING_SIGNATURE)
531+
throw new CodeError('Message type should be "unsigned" when signature policy is StrictNoSign but it was not', codes.ERR_MISSING_SIGNATURE)
532532
}
533533

534534
// @ts-expect-error should not be present
535535
if (message.signature != null) {
536-
throw errcode(new Error('StrictNoSigning: signature should not be present'), codes.ERR_UNEXPECTED_SIGNATURE)
536+
throw new CodeError('StrictNoSigning: signature should not be present', codes.ERR_UNEXPECTED_SIGNATURE)
537537
}
538538

539539
// @ts-expect-error should not be present
540540
if (message.key != null) {
541-
throw errcode(new Error('StrictNoSigning: key should not be present'), codes.ERR_UNEXPECTED_KEY)
541+
throw new CodeError('StrictNoSigning: key should not be present', codes.ERR_UNEXPECTED_KEY)
542542
}
543543

544544
// @ts-expect-error should not be present
545545
if (message.sequenceNumber != null) {
546-
throw errcode(new Error('StrictNoSigning: seqno should not be present'), codes.ERR_UNEXPECTED_SEQNO)
546+
throw new CodeError('StrictNoSigning: seqno should not be present', codes.ERR_UNEXPECTED_SEQNO)
547547
}
548548
break
549549
case 'StrictSign':
550550
if (message.type !== 'signed') {
551-
throw errcode(new Error('Message type should be "signed" when signature policy is StrictSign but it was not'), codes.ERR_MISSING_SIGNATURE)
551+
throw new CodeError('Message type should be "signed" when signature policy is StrictSign but it was not', codes.ERR_MISSING_SIGNATURE)
552552
}
553553

554554
if (message.signature == null) {
555-
throw errcode(new Error('StrictSigning: Signing required and no signature was present'), codes.ERR_MISSING_SIGNATURE)
555+
throw new CodeError('StrictSigning: Signing required and no signature was present', codes.ERR_MISSING_SIGNATURE)
556556
}
557557

558558
if (message.sequenceNumber == null) {
559-
throw errcode(new Error('StrictSigning: Signing required and no sequenceNumber was present'), codes.ERR_MISSING_SEQNO)
559+
throw new CodeError('StrictSigning: Signing required and no sequenceNumber was present', codes.ERR_MISSING_SEQNO)
560560
}
561561

562562
if (!(await verifySignature(message, this.encodeMessage.bind(this)))) {
563-
throw errcode(new Error('StrictSigning: Invalid message signature'), codes.ERR_INVALID_SIGNATURE)
563+
throw new CodeError('StrictSigning: Invalid message signature', codes.ERR_INVALID_SIGNATURE)
564564
}
565565

566566
break
567567
default:
568-
throw errcode(new Error('Cannot validate message: unhandled signature policy'), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
568+
throw new CodeError('Cannot validate message: unhandled signature policy', codes.ERR_UNHANDLED_SIGNATURE_POLICY)
569569
}
570570

571571
const validatorFn = this.topicValidators.get(message.topic)
572572
if (validatorFn != null) {
573573
const result = await validatorFn(from, message)
574574
if (result === TopicValidatorResult.Reject || result === TopicValidatorResult.Ignore) {
575-
throw errcode(new Error('Message validation failed'), codes.ERR_TOPIC_VALIDATOR_REJECT)
575+
throw new CodeError('Message validation failed', codes.ERR_TOPIC_VALIDATOR_REJECT)
576576
}
577577
}
578578
}
@@ -592,7 +592,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
592592
...message
593593
})
594594
default:
595-
throw errcode(new Error('Cannot build message: unhandled signature policy'), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
595+
throw new CodeError('Cannot build message: unhandled signature policy', codes.ERR_UNHANDLED_SIGNATURE_POLICY)
596596
}
597597
}
598598

@@ -603,11 +603,11 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
603603
*/
604604
getSubscribers (topic: string): PeerId[] {
605605
if (!this.started) {
606-
throw errcode(new Error('not started yet'), 'ERR_NOT_STARTED_YET')
606+
throw new CodeError('not started yet', 'ERR_NOT_STARTED_YET')
607607
}
608608

609609
if (topic == null) {
610-
throw errcode(new Error('topic is required'), 'ERR_NOT_VALID_TOPIC')
610+
throw new CodeError('topic is required', 'ERR_NOT_VALID_TOPIC')
611611
}
612612

613613
const peersInTopic = this.topics.get(topic.toString())

src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { sha256 } from 'multiformats/hashes/sha2'
55
import type { Message, PubSubRPCMessage } from '@libp2p/interface-pubsub'
66
import { peerIdFromBytes } from '@libp2p/peer-id'
77
import { codes } from './errors.js'
8-
import errcode from 'err-code'
8+
import { CodeError } from '@libp2p/interfaces/errors'
99

1010
/**
1111
* Generate a random sequence number
@@ -68,7 +68,7 @@ export const ensureArray = function <T> (maybeArray: T | T[]) {
6868

6969
export const toMessage = (message: PubSubRPCMessage): Message => {
7070
if (message.from == null) {
71-
throw errcode(new Error('RPC message was missing from'), codes.ERR_MISSING_FROM)
71+
throw new CodeError('RPC message was missing from', codes.ERR_MISSING_FROM)
7272
}
7373

7474
if (message.sequenceNumber == null || message.from == null || message.signature == null || message.key == null) {

0 commit comments

Comments
 (0)