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:
Adds more context to the errors, which is helpful in debug mode.
Allows the errors to be automatically sent to the user.
Function can be reused for general purpose.
Drawbacks:
If the user has a top-level exception handler in JS (which they should, and this template provides), that exception handler will also print the (less useful) exception and possibly also call Game.notify.
This is at worst just visual clutter, no technical problems.
Option 2: Implement console.error as function () {}.
Benefits:
Avoids double logging and double notifying on wasm bindgen errors.
Less visual clutter.
Drawbacks:
Possibly confusing to beginners.
Misses the information that "wasm_bindgen function threw an exception" which adds the context that the bindings are incorrect.
Option 3: Implement console.error as deferring to console.log.
Benefits:
Makes sure that the information is all logged.
Drawbacks:
Still double logs.
Doesn't allow for easy modification of how things are displayed.
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.
When in debug mode,
wasm-bindgen
expects to be able to throw exceptions when it does certain checks. Its error handler does approximatelyThe screeps environment doesn't expose the web's version of
console
at all to the user, only a mock object inglobal
that only haslog
. This results in wasm bindgen's error handler instead throwing an exceptioncould not find function console.error
which is even more useless.This PR adds that
error
function to the globalconsole
so that wasm bindgen can use it. The current implementation has some drawbacks:Option 1: Implement a proper
console.error
with logging andGame.notify
. (this PR)Benefits:
Drawbacks:
Game.notify
.Option 2: Implement
console.error
asfunction () {}
.Benefits:
Drawbacks:
Option 3: Implement
console.error
as deferring toconsole.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.