From 025ede07707d4925d7e0fac0d86d07e85432aa61 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 8 Aug 2024 12:14:27 +0100 Subject: [PATCH] feat: add .name property to errors (#140) Adds a .name property to errors used as rejection reasons. --- packages/it-byte-stream/src/errors.ts | 7 ++++++ packages/it-byte-stream/src/index.ts | 10 +++++++- .../it-length-prefixed-stream/src/errors.ts | 25 +++++++++++++++++++ .../it-length-prefixed-stream/src/index.ts | 18 ++++++------- 4 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 packages/it-byte-stream/src/errors.ts create mode 100644 packages/it-length-prefixed-stream/src/errors.ts diff --git a/packages/it-byte-stream/src/errors.ts b/packages/it-byte-stream/src/errors.ts new file mode 100644 index 00000000..3808bda1 --- /dev/null +++ b/packages/it-byte-stream/src/errors.ts @@ -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' +} diff --git a/packages/it-byte-stream/src/index.ts b/packages/it-byte-stream/src/index.ts index 4af40ea8..d4fad4b3 100644 --- a/packages/it-byte-stream/src/index.ts +++ b/packages/it-byte-stream/src/index.ts @@ -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 @@ -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' } } @@ -136,7 +144,7 @@ export function byteStream > (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) diff --git a/packages/it-length-prefixed-stream/src/errors.ts b/packages/it-length-prefixed-stream/src/errors.ts new file mode 100644 index 00000000..28e0c09b --- /dev/null +++ b/packages/it-length-prefixed-stream/src/errors.ts @@ -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' +} diff --git a/packages/it-length-prefixed-stream/src/index.ts b/packages/it-length-prefixed-stream/src/index.ts index 165c8586..4ca37702 100644 --- a/packages/it-length-prefixed-stream/src/index.ts +++ b/packages/it-length-prefixed-stream/src/index.ts @@ -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 } @@ -104,8 +96,12 @@ export function lpStream > (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) { @@ -114,7 +110,7 @@ export function lpStream > (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)