Skip to content

Commit

Permalink
NBT Error Parse Info
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Offroaders123 committed Feb 3, 2024
1 parent 2bbeb2d commit 388ac02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
3 changes: 2 additions & 1 deletion src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<RootTag>(value as RootTag,{ rootName, endian }), remaining });
}

return new NBTData<T>(value,{ rootName, endian });
Expand Down

0 comments on commit 388ac02

Please sign in to comment.