Skip to content

Commit

Permalink
feat: add .name property to errors (#140)
Browse files Browse the repository at this point in the history
Adds a .name property to errors used as rejection reasons.
  • Loading branch information
achingbrain authored Aug 8, 2024
1 parent 55262a8 commit 025ede0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
7 changes: 7 additions & 0 deletions packages/it-byte-stream/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* The incoming stream ended before the expected number of bytes were read
*/
export class UnexpectedEOFError extends Error {
name = 'UnexpectedEOFError'
code = 'ERR_UNEXPECTED_EOF'
}
10 changes: 9 additions & 1 deletion packages/it-byte-stream/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@

import { queuelessPushable } from 'it-queueless-pushable'
import { Uint8ArrayList } from 'uint8arraylist'
import { UnexpectedEOFError } from './errors.js'
import type { Duplex } from 'it-stream-types'

/**
* @deprecated This will not be exported in a future release
*/
export class CodeError extends Error {
public readonly code: string

Expand All @@ -34,12 +38,16 @@ export class CodeError extends Error {
}
}

/**
* @deprecated This will not be exported in a future release
*/
export class AbortError extends CodeError {
public readonly type: string

constructor (message: string) {
super(message, 'ABORT_ERR')
this.type = 'aborted'
this.name = 'AbortError'
}
}

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

if (done === true) {
throw new CodeError('unexpected end of input', 'ERR_UNEXPECTED_EOF')
throw new UnexpectedEOFError('unexpected end of input')
}

readBuffer.append(value)
Expand Down
25 changes: 25 additions & 0 deletions packages/it-length-prefixed-stream/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* The reported length of the next data message was not a positive integer
*/
export class InvalidMessageLengthError extends Error {
name = 'InvalidMessageLengthError'
code = 'ERR_INVALID_MSG_LENGTH'
}

/**
* The reported length of the next data message was larger than the configured
* max allowable value
*/
export class InvalidDataLengthError extends Error {
name = 'InvalidDataLengthError'
code = 'ERR_MSG_DATA_TOO_LONG'
}

/**
* The varint used to specify the length of the next data message contained more
* bytes than the configured max allowable value
*/
export class InvalidDataLengthLengthError extends Error {
name = 'InvalidDataLengthLengthError'
code = 'ERR_MSG_LENGTH_TOO_LONG'
}
18 changes: 7 additions & 11 deletions packages/it-length-prefixed-stream/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,9 @@
import { byteStream, type ByteStreamOpts } from 'it-byte-stream'
import * as varint from 'uint8-varint'
import { Uint8ArrayList } from 'uint8arraylist'
import { InvalidDataLengthError, InvalidDataLengthLengthError, InvalidMessageLengthError } from './errors.js'
import type { Duplex } from 'it-stream-types'

class CodeError extends Error {
public readonly code: string

constructor (message: string, code: string) {
super(message)
this.code = code
}
}

export interface AbortOptions {
signal?: AbortSignal
}
Expand Down Expand Up @@ -104,8 +96,12 @@ export function lpStream <Stream extends Duplex<any, any, any>> (duplex: Stream,
throw err
}

if (dataLength < 0) {
throw new InvalidMessageLengthError('Invalid message length')
}

if (opts?.maxLengthLength != null && lengthBuffer.byteLength > opts.maxLengthLength) {
throw new CodeError('message length length too long', 'ERR_MSG_LENGTH_TOO_LONG')
throw new InvalidDataLengthLengthError('message length length too long')
}

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

if (opts?.maxDataLength != null && dataLength > opts.maxDataLength) {
throw new CodeError('message length too long', 'ERR_MSG_DATA_TOO_LONG')
throw new InvalidDataLengthError('message length too long')
}

return bytes.read(dataLength, options)
Expand Down

0 comments on commit 025ede0

Please sign in to comment.