From b3e83f9bcb75be02f20f2b2c067cfe675dd9b252 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 21 Aug 2025 12:05:17 +0300 Subject: [PATCH 1/3] feat: contextful errors on custom payload parse --- src/client/errors.js | 13 +++++++++++++ src/client/pluginChannels.js | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/client/errors.js diff --git a/src/client/errors.js b/src/client/errors.js new file mode 100644 index 00000000..7cf89001 --- /dev/null +++ b/src/client/errors.js @@ -0,0 +1,13 @@ +class CustomPayloadParseError extends Error { + constructor(message, cause, data) { + super(message) + this.name = 'CustomPayloadParseError' + this.cause = cause + this.data = data + this.message = `Custom channel: ${data.channel} - ${message}` + } +} + +module.exports = { + CustomPayloadParseError +} diff --git a/src/client/pluginChannels.js b/src/client/pluginChannels.js index 671eb452..6fb53a82 100644 --- a/src/client/pluginChannels.js +++ b/src/client/pluginChannels.js @@ -2,6 +2,7 @@ const ProtoDef = require('protodef').ProtoDef const minecraft = require('../datatypes/minecraft') const debug = require('debug')('minecraft-protocol') const nbt = require('prismarine-nbt') +const { CustomPayloadParseError } = require('./errors') module.exports = function (client, options) { const mcdata = require('minecraft-data')(options.version || require('../version').defaultVersion) @@ -57,7 +58,8 @@ module.exports = function (client, options) { try { packet.data = proto.parsePacketBuffer(channel, packet.data).data } catch (error) { - client.emit('error', error) + const customError = new CustomPayloadParseError(error.message, error, packet) + client.emit('error', customError) return } } From 5f3ce50d76fce9c988a4a4f7e66c1eed5f356294 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 21 Aug 2025 12:09:49 +0300 Subject: [PATCH 2/3] add to d.ts --- src/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.d.ts b/src/index.d.ts index bf52ca63..62441293 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -8,6 +8,7 @@ import { Transform } from "readable-stream"; import { BinaryLike, KeyObject } from 'crypto'; import { Realm } from "prismarine-realms" import NodeRSA from 'node-rsa'; +import { CustomPayloadParseError } from './client/errors'; type PromiseLike = Promise | void @@ -41,7 +42,7 @@ declare module 'minecraft-protocol' { verifyMessage(publicKey: Buffer | KeyObject, packet: object): boolean reportPlayer(uuid: string, reason: 'FALSE_REPORTING' | 'HATE_SPEECH' | 'TERRORISM_OR_VIOLENT_EXTREMISM' | 'CHILD_SEXUAL_EXPLOITATION_OR_ABUSE' | 'IMMINENT_HARM' | 'NON_CONSENSUAL_INTIMATE_IMAGERY' | 'HARASSMENT_OR_BULLYING' | 'DEFAMATION_IMPERSONATION_FALSE_INFORMATION' | 'SELF_HARM_OR_SUICIDE' | 'ALCOHOL_TOBACCO_DRUGS', signatures: Buffer[], comment?: string): Promise chat(message: string, options?: { timestamp?: BigInt, salt?: BigInt, preview?: BinaryLike, didPreview?: boolean }): void - on(event: 'error', listener: (error: Error) => PromiseLike): this + on(event: 'error', listener: (error: Error | CustomPayloadParseError) => PromiseLike): this on(event: 'packet', handler: (data: any, packetMeta: PacketMeta, buffer: Buffer, fullBuffer: Buffer) => PromiseLike): this on(event: 'raw', handler: (buffer: Buffer, packetMeta: PacketMeta) => PromiseLike): this on(event: 'session', handler: (session: SessionObject) => PromiseLike): this From cb0282602cb56e123472e92d39d586f8cdeafd42 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Thu, 21 Aug 2025 12:12:56 +0300 Subject: [PATCH 3/3] fix --- src/client/errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/errors.js b/src/client/errors.js index 7cf89001..35995fb9 100644 --- a/src/client/errors.js +++ b/src/client/errors.js @@ -1,5 +1,5 @@ class CustomPayloadParseError extends Error { - constructor(message, cause, data) { + constructor (message, cause, data) { super(message) this.name = 'CustomPayloadParseError' this.cause = cause