Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce DecodeError class #165

Merged
merged 1 commit into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ const DEFAULT_MAX_LENGTH = 0xffff_ffff; // uint32_max

const sharedCachedKeyDecoder = new CachedKeyDecoder();

export class DecodeError extends Error {
constructor(message: string) {
super(message);

Object.defineProperty(this, 'name', {
configurable: true,
enumerable: false,
value: this.constructor.name,
});
}
}

export class Decoder<ContextType> {
private totalPos = 0;
private pos = 0;
Expand Down Expand Up @@ -379,7 +391,7 @@ export class Decoder<ContextType> {
const size = this.lookU32();
object = this.decodeExtension(size, 4);
} else {
throw new Error(`Unrecognized type byte: ${prettyByte(headByte)}`);
throw new DecodeError(`Unrecognized type byte: ${prettyByte(headByte)}`);
}

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

state.key = object;
Expand Down Expand Up @@ -451,15 +463,15 @@ export class Decoder<ContextType> {
if (headByte < 0xa0) {
return headByte - 0x90;
} else {
throw new Error(`Unrecognized array type byte: ${prettyByte(headByte)}`);
throw new DecodeError(`Unrecognized array type byte: ${prettyByte(headByte)}`);
}
}
}
}

private pushMapState(size: number) {
if (size > this.maxMapLength) {
throw new Error(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
throw new DecodeError(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
}

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

private pushArrayState(size: number) {
if (size > this.maxArrayLength) {
throw new Error(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
throw new DecodeError(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
}

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

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

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

private decodeBinary(byteLength: number, headOffset: number): Uint8Array {
if (byteLength > this.maxBinLength) {
throw new Error(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
throw new DecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
}

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

private decodeExtension(size: number, headOffset: number): unknown {
if (size > this.maxExtLength) {
throw new Error(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
throw new DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
}

const extType = this.view.getInt8(this.pos + headOffset);
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export { DecodeOptions };
import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync";
export { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream };

import { Decoder } from "./Decoder";
export { Decoder };
import { Decoder, DecodeError } from "./Decoder";
export { Decoder, DecodeError };

import { Encoder } from "./Encoder";
export { Encoder };
Expand Down