rustyscreeps / screeps-starter-rust

Starter Rust AI for Screeps, the JavaScript-based MMO game
MIT License
121 stars 47 forks source link

Add console.error function for wasm_bindgen #45

Closed asquared31415 closed 10 months ago

asquared31415 commented 11 months ago

When in debug mode, wasm-bindgen expects to be able to throw exceptions when it does certain checks. Its error handler does approximately

console.error("JS function threw an exception, but was not marked with catch", error);
throw error;

The screeps environment doesn't expose the web's version of console at all to the user, only a mock object in global that only has log. This results in wasm bindgen's error handler instead throwing an exception could not find function console.error which is even more useless.

This PR adds that error function to the global console so that wasm bindgen can use it. The current implementation has some drawbacks:

Option 1: Implement a proper console.error with logging and Game.notify. (this PR)

Benefits:

Drawbacks:

Option 2: Implement console.error as function () {}.

Benefits:

Drawbacks:

Option 3: Implement console.error as deferring to console.log.

Benefits:

Drawbacks:

Therefore I believe that it's better for the default code that we provide to users have an actual implementation, and users can disable the function if they do not find value in it.

Also I updated the error handler in the starter to use this function. Users probably expect error handling to be somewhat consistent, at least across JS errors.

cc @BlueskyFr who was part of uncovering this issue by being the first one in ages to use debug mode in a specific state that can occur on MMO but uncommonly.