Skip to content

Instantly share code, notes, and snippets.

@zalo
Last active March 29, 2021 19:08
Show Gist options
  • Save zalo/ed68783a3afba3774e0d2411c4bf7cb4 to your computer and use it in GitHub Desktop.
Save zalo/ed68783a3afba3774e0d2411c4bf7cb4 to your computer and use it in GitHub Desktop.
Small Debug Utility for viewing Mobile Console Errors
/** This class provides Debug Utilities. */
class Debug {
/** Reroute Console Errors to the Main Screen (for mobile)
* @param {Worker} worker */
constructor(worker) {
// Route Worker Errors Here
worker.registerCallback("error", this.fakeError.bind(this));
// Intercept Main Window Errors as well
window.addEventListener('error', function (event) {
let errorNode = window.document.createElement("div");
let path = event.filename.split("/");
errorNode.innerHTML = (path[path.length-1] + ":"+ event.lineno + " - " + event.message).fontcolor("red");
window.document.getElementById("info").appendChild(errorNode);
});
// Intercept console.error (TODO: Include console.warn and console.log)
window.realConsoleError = console.error;
console.error = this.fakeError.bind(this);
}
// Log Errors as <div>s over the main viewport
fakeError(...args) {
if (args.length > 0) {
let errorNode = window.document.createElement("div");
errorNode.innerHTML = JSON.stringify(args[0]).fontcolor("red");
window.document.getElementById("info").appendChild(errorNode);
}
window.realConsoleError.apply(console, arguments);
}
}
export { Debug };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment