Skip to content

Commit 025ede0

Browse files
authored
feat: add .name property to errors (#140)
Adds a .name property to errors used as rejection reasons.
1 parent 55262a8 commit 025ede0

4 files changed

Lines changed: 48 additions & 12 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* The incoming stream ended before the expected number of bytes were read
3+
*/
4+
export class UnexpectedEOFError extends Error {
5+
name = 'UnexpectedEOFError'
6+
code = 'ERR_UNEXPECTED_EOF'
7+
}

packages/it-byte-stream/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323

2424
import { queuelessPushable } from 'it-queueless-pushable'
2525
import { Uint8ArrayList } from 'uint8arraylist'
26+
import { UnexpectedEOFError } from './errors.js'
2627
import type { Duplex } from 'it-stream-types'
2728

29+
/**
30+
* @deprecated This will not be exported in a future release
31+
*/
2832
export class CodeError extends Error {
2933
public readonly code: string
3034

@@ -34,12 +38,16 @@ export class CodeError extends Error {
3438
}
3539
}
3640

41+
/**
42+
* @deprecated This will not be exported in a future release
43+
*/
3744
export class AbortError extends CodeError {
3845
public readonly type: string
3946

4047
constructor (message: string) {
4148
super(message, 'ABORT_ERR')
4249
this.type = 'aborted'
50+
this.name = 'AbortError'
4351
}
4452
}
4553

@@ -136,7 +144,7 @@ export function byteStream <Stream extends Duplex<any, any, any>> (duplex: Strea
136144
])
137145

138146
if (done === true) {
139-
throw new CodeError('unexpected end of input', 'ERR_UNEXPECTED_EOF')
147+
throw new UnexpectedEOFError('unexpected end of input')
140148
}
141149

142150
readBuffer.append(value)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* The reported length of the next data message was not a positive integer
3+
*/
4+
export class InvalidMessageLengthError extends Error {
5+
name = 'InvalidMessageLengthError'
6+
code = 'ERR_INVALID_MSG_LENGTH'
7+
}
8+
9+
/**
10+
* The reported length of the next data message was larger than the configured
11+
* max allowable value
12+
*/
13+
export class InvalidDataLengthError extends Error {
14+
name = 'InvalidDataLengthError'
15+
code = 'ERR_MSG_DATA_TOO_LONG'
16+
}
17+
18+
/**
19+
* The varint used to specify the length of the next data message contained more
20+
* bytes than the configured max allowable value
21+
*/
22+
export class InvalidDataLengthLengthError extends Error {
23+
name = 'InvalidDataLengthLengthError'
24+
code = 'ERR_MSG_LENGTH_TOO_LONG'
25+
}

packages/it-length-prefixed-stream/src/index.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,9 @@
2626
import { byteStream, type ByteStreamOpts } from 'it-byte-stream'
2727
import * as varint from 'uint8-varint'
2828
import { Uint8ArrayList } from 'uint8arraylist'
29+
import { InvalidDataLengthError, InvalidDataLengthLengthError, InvalidMessageLengthError } from './errors.js'
2930
import type { Duplex } from 'it-stream-types'
3031

31-
class CodeError extends Error {
32-
public readonly code: string
33-
34-
constructor (message: string, code: string) {
35-
super(message)
36-
this.code = code
37-
}
38-
}
39-
4032
export interface AbortOptions {
4133
signal?: AbortSignal
4234
}
@@ -104,8 +96,12 @@ export function lpStream <Stream extends Duplex<any, any, any>> (duplex: Stream,
10496
throw err
10597
}
10698

99+
if (dataLength < 0) {
100+
throw new InvalidMessageLengthError('Invalid message length')
101+
}
102+
107103
if (opts?.maxLengthLength != null && lengthBuffer.byteLength > opts.maxLengthLength) {
108-
throw new CodeError('message length length too long', 'ERR_MSG_LENGTH_TOO_LONG')
104+
throw new InvalidDataLengthLengthError('message length length too long')
109105
}
110106

111107
if (dataLength > -1) {
@@ -114,7 +110,7 @@ export function lpStream <Stream extends Duplex<any, any, any>> (duplex: Stream,
114110
}
115111

116112
if (opts?.maxDataLength != null && dataLength > opts.maxDataLength) {
117-
throw new CodeError('message length too long', 'ERR_MSG_DATA_TOO_LONG')
113+
throw new InvalidDataLengthError('message length too long')
118114
}
119115

120116
return bytes.read(dataLength, options)

0 commit comments

Comments
 (0)