Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle TypeError in #onError and removeAllListeners in digging.js #3577

Closed

Conversation

DimonChichikalov
Copy link

Added checks before removing listeners to prevent crash.

@extremeheat
Copy link
Member

Why are these checks needed ?

if (bot.listenerCount('diggingCompleted') > 0) {
bot.removeAllListeners('diggingCompleted');
}
if (typeof bot.stopDigging === 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what situation is this not defined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happens when the bot dies. This is the full error:
200 | #onClose(e) {

201 | this.#onExitPromise = e.code, this.emit("exit", e.code);

202 | }

203 | #onError(event) {

204 | let error = event?.error;

205 | error = new Error(event.message, { cause: event });

                ^

error: 208 |

209 | await diggingTask.promise

210 | }

211 |

212 | bot.on('death', () => {

213 | bot.removeAllListeners('diggingAborted');

          ^

TypeError: undefined is not an object (evaluating 'this._events')

  at removeAllListeners (node:events:244:25)

  at <anonymous> (/home/dimonchi/Project-Archive-Minecraft/node_modules/mineflayer/lib/plugins/digging.js:213:9)

  at emit (node:events:84:22)

  at <anonymous> (/home/dimonchi/Project-Archive-Minecraft/node_modules/mineflayer/lib/plugins/health.js:25:13)

  at emit (node:events:90:22)

  at emitPacket (/home/dimonchi/Project-Archive-Minecraft/node_modules/minecraft-protocol/src/client.js:84:12)

  at <anonymous> (/home/dimonchi/Project-Archive-Minecraft/node_modules/minecraft-protocol/src/client.js:113:9)

  at emit (node:events:87:22)

  at addChunk (/home/dimonchi/Project-Archive-Minecraft/node_modules/readable-stream/lib/internal/streams/readable.js:323:12)

  at readableAddChunk (/home/dimonchi/Project-Archive-Minecraft/node_modules/readable-stream/lib/internal/streams/readable.js:300:9)



  at #onError (node:worker_threads:205:15)

Aborted (core dumped)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a problem with your code, not mineflayer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants