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

Commit c340711

Browse files
committed
chore!: replace errcode with CodeError
1 parent cb1489b commit c340711

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/error.ts

+24-25
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

3131
export function connectionClosedError (state: RTCPeerConnectionState, msg: string) {
32-
return errCode(new ConnectionClosedError(state, msg), codes.ERR_CONNECTION_CLOSED)
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

4242
export function dataChannelError (streamLabel: string, msg: string) {
43-
return errCode(new DataChannelError(streamLabel, msg), codes.ERR_DATA_CHANNEL)
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

5353
export function inappropriateMultiaddr (msg: string) {
54-
return errCode(new InappropriateMultiaddrError(msg), codes.ERR_INVALID_MULTIADDR)
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

6464
export function invalidArgument (msg: string) {
65-
return errCode(new InvalidArgumentError(msg), codes.ERR_INVALID_PARAMETERS)
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

7575
export function invalidFingerprint (fingerprint: string, source: string) {
76-
return errCode(new InvalidFingerprintError(fingerprint, source), codes.ERR_INVALID_FINGERPRINT)
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

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

9090
export class OverStreamLimitError extends WebRTCTransportError {
91-
constructor (msg: string) {
92-
super(msg)
91+
constructor (dir: Direction, proto: string) {
92+
const code = dir === 'inbound' ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS
93+
super(`${dir} stream limit reached for protocol - ${proto}`, code)
9394
this.name = 'WebRTC/OverStreamLimitError'
9495
}
9596
}
9697

9798
export function overStreamLimit (dir: Direction, proto: string) {
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)
99+
return new OverStreamLimitError(dir, 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

109109
export function unimplemented (methodName: string) {
110-
return errCode(new UnimplementedError(methodName), codes.ERR_NOT_IMPLEMENTED)
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

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

0 commit comments

Comments
 (0)