diff --git a/src/client/errors.js b/src/client/errors.js new file mode 100644 index 00000000..35995fb9 --- /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 } } 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