From 388ac02b0ed79e79d227c12e6ffb1195155561ef Mon Sep 17 00:00:00 2001 From: Offroaders123 <65947371+Offroaders123@users.noreply.github.com> Date: Fri, 2 Feb 2024 17:54:47 -0800 Subject: [PATCH] NBT Error Parse Info Working on figuring out the concepts behind making your own error handlers, in that I can provide the missing metadata I need to be able to get without needing to parse the error message itself. This is a big of a weird setup at the moment, since I don't quite know the correct way to structure these kinds of extensions. https://developer.mozilla.org/en-US/docs/Web/API/DOMException https://stackoverflow.com/questions/5136727/manually-artificially-throwing-a-domexception-with-javascript https://javascript.info/custom-errors https://stackoverflow.com/questions/67558074/how-to-throw-custom-errors https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause #31 --- src/error.ts | 20 ++++++++++++++++++++ src/index.ts | 1 + src/read.ts | 3 ++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/error.ts diff --git a/src/error.ts b/src/error.ts new file mode 100644 index 0000000..9e3e449 --- /dev/null +++ b/src/error.ts @@ -0,0 +1,20 @@ +import type { NBTData } from "./format.js"; + +export interface NBTErrorOptions extends ErrorOptions { + byteOffset: number; + cause: NBTData; + remaining: number; +} + +export class NBTError extends Error { + byteOffset: number; + override cause: NBTData; + remaining: number; + + constructor(message: string, options: NBTErrorOptions) { + super(message,options); + this.byteOffset = options.byteOffset; + this.cause = options.cause; + this.remaining = options.remaining; + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 1481f7e..8411ade 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,4 +5,5 @@ export * from "./stringify.js"; export * from "./format.js"; export * from "./tag.js"; export * from "./primitive.js"; +export * from "./error.js"; export * from "./compression.js"; \ No newline at end of file diff --git a/src/read.ts b/src/read.ts index 69fe1cc..89c0d18 100644 --- a/src/read.ts +++ b/src/read.ts @@ -2,6 +2,7 @@ import { NBTData } from "./format.js"; import { Int8, Int16, Int32, Float32 } from "./primitive.js"; import { TAG, TAG_TYPE } from "./tag.js"; import { decompress } from "./compression.js"; +import { NBTError } from "./error.js"; import type { RootName, Endian, Compression, BedrockLevel } from "./format.js"; import type { Tag, RootTag, RootTagLike, ByteTag, ShortTag, IntTag, LongTag, FloatTag, DoubleTag, StringTag, ByteArrayTag, ListTag, CompoundTag, IntArrayTag, LongArrayTag } from "./tag.js"; @@ -199,7 +200,7 @@ export class NBTReader { if (strict && data.byteLength > this.#byteOffset){ const remaining = data.byteLength - this.#byteOffset; - throw new Error(`Encountered unexpected End tag at byte offset ${this.#byteOffset}, ${remaining} unread bytes remaining`); + throw new NBTError(`Encountered unexpected End tag at byte offset ${this.#byteOffset}, ${remaining} unread bytes remaining`,{ byteOffset: this.#byteOffset, cause: new NBTData(value as RootTag,{ rootName, endian }), remaining }); } return new NBTData(value,{ rootName, endian });