Skip to content

Commit

Permalink
Merge pull request msgpack#165 from msgpack/decode_error_class
Browse files Browse the repository at this point in the history
introduce DecodeError class
  • Loading branch information
gfx authored Mar 21, 2021
2 parents a3df768 + 07b85b5 commit 0ff708a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
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

0 comments on commit 0ff708a

Please sign in to comment.