Skip to content

Commit 81ebe4e

Browse files
authored
fix!: remove CodeError class (#2688)
The `CodeError` class has been replaced with specific error subclasses with named constructors that use a `.name` property. BREAKING CHANGE: instead of `CodeError`, use `TimeoutError`, `UnexpectedPeerError`, etc
1 parent 5214dec commit 81ebe4e

File tree

9 files changed

+4059
-148
lines changed

9 files changed

+4059
-148
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,8 @@
5151
"doc",
5252
"interop",
5353
"packages/*"
54-
]
54+
],
55+
"overrides": {
56+
"protons-runtime": "^5.5.0"
57+
}
5558
}

packages/interface/src/errors.ts

Lines changed: 150 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,162 +4,224 @@
44
* AbortSignal.
55
*/
66
export class AbortError extends Error {
7+
static name = 'AbortError'
8+
79
constructor (message: string = 'The operation was aborted') {
810
super(message)
911
this.name = 'AbortError'
1012
}
1113
}
1214

1315
/**
14-
* @deprecated
16+
* Thrown when a remote Peer ID does not match the expected one
1517
*/
16-
export class CodeError<T extends Record<string, any> = Record<string, never>> extends Error {
17-
public readonly props: T
18-
19-
constructor (
20-
message: string,
21-
public readonly code: string,
22-
props?: T
23-
) {
24-
super(message)
25-
26-
this.name = props?.name ?? 'CodeError'
27-
this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions
28-
}
29-
}
30-
31-
/**
32-
* @deprecated
33-
*/
34-
export class AggregateCodeError<T extends Record<string, any> = Record<string, never>> extends AggregateError {
35-
public readonly props: T
36-
37-
constructor (
38-
errors: Error[],
39-
message: string,
40-
public readonly code: string,
41-
props?: T
42-
) {
43-
super(errors, message)
44-
45-
this.name = props?.name ?? 'AggregateCodeError'
46-
this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions
47-
}
48-
}
49-
5018
export class UnexpectedPeerError extends Error {
19+
static name = 'UnexpectedPeerError'
20+
5121
constructor (message = 'Unexpected Peer') {
5222
super(message)
5323
this.name = 'UnexpectedPeerError'
5424
}
5525
}
5626

27+
/**
28+
* Thrown when a crypto exchange fails
29+
*/
5730
export class InvalidCryptoExchangeError extends Error {
31+
static name = 'InvalidCryptoExchangeError'
32+
5833
constructor (message = 'Invalid crypto exchange') {
5934
super(message)
6035
this.name = 'InvalidCryptoExchangeError'
6136
}
6237
}
6338

39+
/**
40+
* Thrown when invalid parameters are passed to a function or method call
41+
*/
6442
export class InvalidParametersError extends Error {
43+
static name = 'InvalidParametersError'
44+
6545
constructor (message = 'Invalid parameters') {
6646
super(message)
6747
this.name = 'InvalidParametersError'
6848
}
6949
}
7050

51+
/**
52+
* Thrown when a public key is invalid
53+
*/
7154
export class InvalidPublicKeyError extends Error {
55+
static name = 'InvalidPublicKeyError'
56+
7257
constructor (message = 'Invalid public key') {
7358
super(message)
7459
this.name = 'InvalidPublicKeyError'
7560
}
7661
}
7762

63+
/**
64+
* Thrown when a private key is invalid
65+
*/
7866
export class InvalidPrivateKeyError extends Error {
67+
static name = 'InvalidPrivateKeyError'
68+
7969
constructor (message = 'Invalid private key') {
8070
super(message)
8171
this.name = 'InvalidPrivateKeyError'
8272
}
8373
}
8474

75+
/**
76+
* Thrown when a operation is unsupported
77+
*/
8578
export class UnsupportedOperationError extends Error {
79+
static name = 'UnsupportedOperationError'
80+
8681
constructor (message = 'Unsupported operation') {
8782
super(message)
8883
this.name = 'UnsupportedOperationError'
8984
}
9085
}
9186

87+
/**
88+
* Thrown when a connection is closing
89+
*/
9290
export class ConnectionClosingError extends Error {
91+
static name = 'ConnectionClosingError'
92+
9393
constructor (message = 'The connection is closing') {
9494
super(message)
9595
this.name = 'ConnectionClosingError'
9696
}
9797
}
9898

99+
/**
100+
* Thrown when a connection is closed
101+
*/
99102
export class ConnectionClosedError extends Error {
103+
static name = 'ConnectionClosedError'
104+
100105
constructor (message = 'The connection is closed') {
101106
super(message)
102107
this.name = 'ConnectionClosedError'
103108
}
104109
}
105110

111+
/**
112+
* Thrown when a connection fails
113+
*/
106114
export class ConnectionFailedError extends Error {
115+
static name = 'ConnectionFailedError'
116+
107117
constructor (message = 'Connection failed') {
108118
super(message)
109119
this.name = 'ConnectionFailedError'
110120
}
111121
}
112122

123+
/**
124+
* Thrown when the muxer is closed and an attempt to open a stream occurs
125+
*/
126+
export class MuxerClosedError extends Error {
127+
static name = 'MuxerClosedError'
128+
129+
constructor (message = 'The muxer is closed') {
130+
super(message)
131+
this.name = 'MuxerClosedError'
132+
}
133+
}
134+
135+
/**
136+
* Thrown when a protocol stream is reset by the remote muxer
137+
*/
113138
export class StreamResetError extends Error {
139+
static name = 'StreamResetError'
140+
114141
constructor (message = 'The stream has been reset') {
115142
super(message)
116143
this.name = 'StreamResetError'
117144
}
118145
}
119146

147+
/**
148+
* Thrown when a stream is in an invalid state
149+
*/
120150
export class StreamStateError extends Error {
151+
static name = 'StreamStateError'
152+
121153
constructor (message = 'The stream is in an invalid state') {
122154
super(message)
123155
this.name = 'StreamStateError'
124156
}
125157
}
126158

159+
/**
160+
* Thrown when a value could not be found
161+
*/
127162
export class NotFoundError extends Error {
163+
static name = 'NotFoundError'
164+
128165
constructor (message = 'Not found') {
129166
super(message)
130167
this.name = 'NotFoundError'
131168
}
132169
}
133170

171+
/**
172+
* Thrown when an invalid peer ID is encountered
173+
*/
134174
export class InvalidPeerIdError extends Error {
175+
static name = 'InvalidPeerIdError'
176+
135177
constructor (message = 'Invalid PeerID') {
136178
super(message)
137179
this.name = 'InvalidPeerIdError'
138180
}
139181
}
140182

183+
/**
184+
* Thrown when an invalid multiaddr is encountered
185+
*/
141186
export class InvalidMultiaddrError extends Error {
187+
static name = 'InvalidMultiaddrError'
188+
142189
constructor (message = 'Invalid multiaddr') {
143190
super(message)
144191
this.name = 'InvalidMultiaddrError'
145192
}
146193
}
147194

195+
/**
196+
* Thrown when an invalid CID is encountered
197+
*/
148198
export class InvalidCIDError extends Error {
199+
static name = 'InvalidCIDError'
200+
149201
constructor (message = 'Invalid CID') {
150202
super(message)
151203
this.name = 'InvalidCIDError'
152204
}
153205
}
154206

207+
/**
208+
* Thrown when an invalid multihash is encountered
209+
*/
155210
export class InvalidMultihashError extends Error {
211+
static name = 'InvalidMultihashError'
212+
156213
constructor (message = 'Invalid Multihash') {
157214
super(message)
158215
this.name = 'InvalidMultihashError'
159216
}
160217
}
161218

219+
/**
220+
* Thrown when a protocol is not supported
221+
*/
162222
export class UnsupportedProtocolError extends Error {
223+
static name = 'UnsupportedProtocolError'
224+
163225
constructor (message = 'Unsupported protocol error') {
164226
super(message)
165227
this.name = 'UnsupportedProtocolError'
@@ -170,69 +232,120 @@ export class UnsupportedProtocolError extends Error {
170232
* An invalid or malformed message was encountered during a protocol exchange
171233
*/
172234
export class InvalidMessageError extends Error {
235+
static name = 'InvalidMessageError'
236+
173237
constructor (message = 'Invalid message') {
174238
super(message)
175239
this.name = 'InvalidMessageError'
176240
}
177241
}
178242

243+
/**
244+
* Thrown when a remote peer sends a structurally valid message that does not
245+
* comply with the protocol
246+
*/
179247
export class ProtocolError extends Error {
248+
static name = 'ProtocolError'
249+
180250
constructor (message = 'Protocol error') {
181251
super(message)
182252
this.name = 'ProtocolError'
183253
}
184254
}
185255

256+
/**
257+
* Throw when an operation times out
258+
*/
186259
export class TimeoutError extends Error {
260+
static name = 'TimeoutError'
261+
187262
constructor (message = 'Timed out') {
188263
super(message)
189264
this.name = 'TimeoutError'
190265
}
191266
}
192267

268+
/**
269+
* Thrown when a startable component is interacted with but it has not been
270+
* started yet
271+
*/
193272
export class NotStartedError extends Error {
273+
static name = 'NotStartedError'
274+
194275
constructor (message = 'Not started') {
195276
super(message)
196277
this.name = 'NotStartedError'
197278
}
198279
}
199280

281+
/**
282+
* Thrown when a component is started that has already been started
283+
*/
200284
export class AlreadyStartedError extends Error {
285+
static name = 'AlreadyStartedError'
286+
201287
constructor (message = 'Already started') {
202288
super(message)
203289
this.name = 'AlreadyStartedError'
204290
}
205291
}
206292

293+
/**
294+
* Thrown when dialing an address failed
295+
*/
207296
export class DialError extends Error {
297+
static name = 'DialError'
298+
208299
constructor (message = 'Dial error') {
209300
super(message)
210301
this.name = 'DialError'
211302
}
212303
}
213304

305+
/**
306+
* Thrown when listening on an address failed
307+
*/
214308
export class ListenError extends Error {
309+
static name = 'ListenError'
310+
215311
constructor (message = 'Listen error') {
216312
super(message)
217313
this.name = 'ListenError'
218314
}
219315
}
220316

317+
/**
318+
* This error is thrown when a limited connection is encountered, i.e. if the
319+
* user tried to open a stream on a connection for a protocol that is not
320+
* configured to run over limited connections.
321+
*/
221322
export class LimitedConnectionError extends Error {
323+
static name = 'LimitedConnectionError'
324+
222325
constructor (message = 'Limited connection') {
223326
super(message)
224327
this.name = 'LimitedConnectionError'
225328
}
226329
}
227330

331+
/**
332+
* This error is thrown where there are too many inbound protocols streams open
333+
*/
228334
export class TooManyInboundProtocolStreamsError extends Error {
335+
static name = 'TooManyInboundProtocolStreamsError'
336+
229337
constructor (message = 'Too many inbound protocol streams') {
230338
super(message)
231339
this.name = 'TooManyInboundProtocolStreamsError'
232340
}
233341
}
234342

343+
/**
344+
* This error is thrown where there are too many outbound protocols streams open
345+
*/
235346
export class TooManyOutboundProtocolStreamsError extends Error {
347+
static name = 'TooManyOutboundProtocolStreamsError'
348+
236349
constructor (message = 'Too many outbound protocol streams') {
237350
super(message)
238351
this.name = 'TooManyOutboundProtocolStreamsError'
@@ -243,6 +356,8 @@ export class TooManyOutboundProtocolStreamsError extends Error {
243356
* Thrown when and attempt to operate on an unsupported key was made
244357
*/
245358
export class UnsupportedKeyTypeError extends Error {
359+
static name = 'UnsupportedKeyTypeError'
360+
246361
constructor (message = 'Unsupported key type') {
247362
super(message)
248363
this.name = 'UnsupportedKeyTypeError'

0 commit comments

Comments
 (0)