Skip to content

Commit 07b85b5

Browse files
committed
introduce DecodeError class
1 parent a3df768 commit 07b85b5

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/Decoder.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ const DEFAULT_MAX_LENGTH = 0xffff_ffff; // uint32_max
6060

6161
const sharedCachedKeyDecoder = new CachedKeyDecoder();
6262

63+
export class DecodeError extends Error {
64+
constructor(message: string) {
65+
super(message);
66+
67+
Object.defineProperty(this, 'name', {
68+
configurable: true,
69+
enumerable: false,
70+
value: this.constructor.name,
71+
});
72+
}
73+
}
74+
6375
export class Decoder<ContextType> {
6476
private totalPos = 0;
6577
private pos = 0;
@@ -379,7 +391,7 @@ export class Decoder<ContextType> {
379391
const size = this.lookU32();
380392
object = this.decodeExtension(size, 4);
381393
} else {
382-
throw new Error(`Unrecognized type byte: ${prettyByte(headByte)}`);
394+
throw new DecodeError(`Unrecognized type byte: ${prettyByte(headByte)}`);
383395
}
384396

385397
this.complete();
@@ -399,7 +411,7 @@ export class Decoder<ContextType> {
399411
}
400412
} else if (state.type === State.MAP_KEY) {
401413
if (!isValidMapKeyType(object)) {
402-
throw new Error("The type of key must be string or number but " + typeof object);
414+
throw new DecodeError("The type of key must be string or number but " + typeof object);
403415
}
404416

405417
state.key = object;
@@ -451,15 +463,15 @@ export class Decoder<ContextType> {
451463
if (headByte < 0xa0) {
452464
return headByte - 0x90;
453465
} else {
454-
throw new Error(`Unrecognized array type byte: ${prettyByte(headByte)}`);
466+
throw new DecodeError(`Unrecognized array type byte: ${prettyByte(headByte)}`);
455467
}
456468
}
457469
}
458470
}
459471

460472
private pushMapState(size: number) {
461473
if (size > this.maxMapLength) {
462-
throw new Error(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
474+
throw new DecodeError(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
463475
}
464476

465477
this.stack.push({
@@ -473,7 +485,7 @@ export class Decoder<ContextType> {
473485

474486
private pushArrayState(size: number) {
475487
if (size > this.maxArrayLength) {
476-
throw new Error(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
488+
throw new DecodeError(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
477489
}
478490

479491
this.stack.push({
@@ -486,7 +498,7 @@ export class Decoder<ContextType> {
486498

487499
private decodeUtf8String(byteLength: number, headerOffset: number): string {
488500
if (byteLength > this.maxStrLength) {
489-
throw new Error(`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`);
501+
throw new DecodeError(`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`);
490502
}
491503

492504
if (this.bytes.byteLength < this.pos + headerOffset + byteLength) {
@@ -516,7 +528,7 @@ export class Decoder<ContextType> {
516528

517529
private decodeBinary(byteLength: number, headOffset: number): Uint8Array {
518530
if (byteLength > this.maxBinLength) {
519-
throw new Error(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
531+
throw new DecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
520532
}
521533

522534
if (!this.hasRemaining(byteLength + headOffset)) {
@@ -531,7 +543,7 @@ export class Decoder<ContextType> {
531543

532544
private decodeExtension(size: number, headOffset: number): unknown {
533545
if (size > this.maxExtLength) {
534-
throw new Error(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
546+
throw new DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
535547
}
536548

537549
const extType = this.view.getInt8(this.pos + headOffset);

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export { DecodeOptions };
1313
import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync";
1414
export { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream };
1515

16-
import { Decoder } from "./Decoder";
17-
export { Decoder };
16+
import { Decoder, DecodeError } from "./Decoder";
17+
export { Decoder, DecodeError };
1818

1919
import { Encoder } from "./Encoder";
2020
export { Encoder };

0 commit comments

Comments
 (0)