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

Commit db28d7c

Browse files
committed
chore: replace err-code with CodeError
deps: remove err-code replace errcode with CodeError remove breaking changes fix linter errors
1 parent 13e3927 commit db28d7c

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@
140140
"@libp2p/interface-peer-id": "^2.0.0",
141141
"@libp2p/interface-stream-muxer": "^3.0.0",
142142
"@libp2p/interface-transport": "^2.0.0",
143+
"@libp2p/interfaces": "^3.2.0",
143144
"@libp2p/logger": "^2.0.0",
144145
"@libp2p/peer-id": "^2.0.0",
145146
"@multiformats/multiaddr": "^11.0.3",
146147
"@protobuf-ts/runtime": "^2.8.0",
147-
"err-code": "^3.0.1",
148148
"it-length-prefixed": "^8.0.3",
149149
"it-merge": "^2.0.0",
150150
"it-pipe": "^2.0.4",

src/error.ts

+32-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import errCode from 'err-code'
1+
import { CodeError } from '@libp2p/interfaces/errors'
22
import type { Direction } from '@libp2p/interface-connection'
33

44
export enum codes {
@@ -14,110 +14,109 @@ export enum codes {
1414
ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS',
1515
}
1616

17-
export class WebRTCTransportError extends Error {
18-
constructor (msg: string) {
19-
super(`WebRTC transport error: ${msg}`)
17+
export class WebRTCTransportError extends CodeError {
18+
constructor (msg: string, code?: string) {
19+
super(`WebRTC transport error: ${msg}`, code ?? '')
2020
this.name = 'WebRTCTransportError'
2121
}
2222
}
2323

2424
export class ConnectionClosedError extends WebRTCTransportError {
2525
constructor (state: RTCPeerConnectionState, msg: string) {
26-
super(`peerconnection moved to state: ${state}: ${msg}`)
26+
super(`peerconnection moved to state: ${state}: ${msg}`, codes.ERR_CONNECTION_CLOSED)
2727
this.name = 'WebRTC/ConnectionClosed'
2828
}
2929
}
3030

31-
export function connectionClosedError (state: RTCPeerConnectionState, msg: string): Error {
32-
return errCode(new ConnectionClosedError(state, msg), codes.ERR_CONNECTION_CLOSED)
31+
export function connectionClosedError (state: RTCPeerConnectionState, msg: string): ConnectionClosedError {
32+
return new ConnectionClosedError(state, msg)
3333
}
3434

3535
export class DataChannelError extends WebRTCTransportError {
3636
constructor (streamLabel: string, msg: string) {
37-
super(`[stream: ${streamLabel}] data channel error: ${msg}`)
37+
super(`[stream: ${streamLabel}] data channel error: ${msg}`, codes.ERR_DATA_CHANNEL)
3838
this.name = 'WebRTC/DataChannelError'
3939
}
4040
}
4141

42-
export function dataChannelError (streamLabel: string, msg: string): Error {
43-
return errCode(new DataChannelError(streamLabel, msg), codes.ERR_DATA_CHANNEL)
42+
export function dataChannelError (streamLabel: string, msg: string): DataChannelError {
43+
return new DataChannelError(streamLabel, msg)
4444
}
4545

4646
export class InappropriateMultiaddrError extends WebRTCTransportError {
4747
constructor (msg: string) {
48-
super(`There was a problem with the Multiaddr which was passed in: ${msg}`)
48+
super(`There was a problem with the Multiaddr which was passed in: ${msg}`, codes.ERR_INVALID_MULTIADDR)
4949
this.name = 'WebRTC/InappropriateMultiaddrError'
5050
}
5151
}
5252

53-
export function inappropriateMultiaddr (msg: string): Error {
54-
return errCode(new InappropriateMultiaddrError(msg), codes.ERR_INVALID_MULTIADDR)
53+
export function inappropriateMultiaddr (msg: string): InappropriateMultiaddrError {
54+
return new InappropriateMultiaddrError(msg)
5555
}
5656

5757
export class InvalidArgumentError extends WebRTCTransportError {
5858
constructor (msg: string) {
59-
super(`There was a problem with a provided argument: ${msg}`)
59+
super(`There was a problem with a provided argument: ${msg}`, codes.ERR_INVALID_PARAMETERS)
6060
this.name = 'WebRTC/InvalidArgumentError'
6161
}
6262
}
6363

64-
export function invalidArgument (msg: string): Error {
65-
return errCode(new InvalidArgumentError(msg), codes.ERR_INVALID_PARAMETERS)
64+
export function invalidArgument (msg: string): InvalidArgumentError {
65+
return new InvalidArgumentError(msg)
6666
}
6767

6868
export class InvalidFingerprintError extends WebRTCTransportError {
6969
constructor (fingerprint: string, source: string) {
70-
super(`Invalid fingerprint "${fingerprint}" within ${source}`)
70+
super(`Invalid fingerprint "${fingerprint}" within ${source}`, codes.ERR_INVALID_FINGERPRINT)
7171
this.name = 'WebRTC/InvalidFingerprintError'
7272
}
7373
}
7474

75-
export function invalidFingerprint (fingerprint: string, source: string): Error {
76-
return errCode(new InvalidFingerprintError(fingerprint, source), codes.ERR_INVALID_FINGERPRINT)
75+
export function invalidFingerprint (fingerprint: string, source: string): InvalidFingerprintError {
76+
return new InvalidFingerprintError(fingerprint, source)
7777
}
7878

7979
export class OperationAbortedError extends WebRTCTransportError {
8080
constructor (context: string, abortReason: string) {
81-
super(`Signalled to abort because (${abortReason}}) ${context}`)
81+
super(`Signalled to abort because (${abortReason}}) ${context}`, codes.ERR_ALREADY_ABORTED)
8282
this.name = 'WebRTC/OperationAbortedError'
8383
}
8484
}
8585

86-
export function operationAborted (context: string, reason: string): Error {
87-
return errCode(new OperationAbortedError(context, reason), codes.ERR_ALREADY_ABORTED)
86+
export function operationAborted (context: string, reason: string): OperationAbortedError {
87+
return new OperationAbortedError(context, reason)
8888
}
8989

9090
export class OverStreamLimitError extends WebRTCTransportError {
9191
constructor (msg: string) {
92-
super(msg)
92+
const code = msg.startsWith('inbound') ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS
93+
super(msg, code)
9394
this.name = 'WebRTC/OverStreamLimitError'
9495
}
9596
}
9697

97-
export function overStreamLimit (dir: Direction, proto: string): Error {
98-
const code = dir === 'inbound' ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS
99-
return errCode(new OverStreamLimitError(`${dir} stream limit reached for protocol - ${proto}`), code)
98+
export function overStreamLimit (dir: Direction, proto: string): OverStreamLimitError {
99+
return new OverStreamLimitError(`${dir} stream limit reached for protocol - ${proto}`)
100100
}
101101

102102
export class UnimplementedError extends WebRTCTransportError {
103103
constructor (methodName: string) {
104-
super(`A method (${methodName}) was called though it has been intentionally left unimplemented.`)
104+
super(`A method (${methodName}) was called though it has been intentionally left unimplemented.`, codes.ERR_NOT_IMPLEMENTED)
105105
this.name = 'WebRTC/UnimplementedError'
106106
}
107107
}
108108

109-
export function unimplemented (methodName: string): Error {
110-
return errCode(new UnimplementedError(methodName), codes.ERR_NOT_IMPLEMENTED)
109+
export function unimplemented (methodName: string): UnimplementedError {
110+
return new UnimplementedError(methodName)
111111
}
112112

113113
export class UnsupportedHashAlgorithmError extends WebRTCTransportError {
114114
constructor (algo: string) {
115-
const msg = `unsupported hash algorithm: ${algo}`
116-
super(msg)
115+
super(`unsupported hash algorithm: ${algo}`, codes.ERR_HASH_NOT_SUPPORTED)
117116
this.name = 'WebRTC/UnsupportedHashAlgorithmError'
118117
}
119118
}
120119

121-
export function unsupportedHashAlgorithm (algorithm: string): Error {
122-
return errCode(new UnsupportedHashAlgorithmError(algorithm), codes.ERR_HASH_NOT_SUPPORTED)
120+
export function unsupportedHashAlgorithm (algorithm: string): UnsupportedHashAlgorithmError {
121+
return new UnsupportedHashAlgorithmError(algorithm)
123122
}

0 commit comments

Comments
 (0)