I'm trying to get all of the console.error
messages caught by dedicated error handler (which is supposed to trigger certain actions upon some specific error) and I'm trying to leverage window.onerror
callback for that purpose.
So far, I can't succeed even with my basic set-up, like
listener.js
(function () {
window.onerror = function (msg) {
console.log("I got the error:", msg);
};
})();
and
thrower.js
(function () {
console.error("sample error");
})();
both are loaded as a static assets from within <head>
section of the very basic
index.html
<!DOCTYPE html>
<html>
<head>
<script src="assets/listen.js"></script>
<script src="assets/throw.js"></script>
</head>
<body>
here I go
</body>
</html>
and served by trivial express app:
index.js
import express from "express";
import { join } from "path";
const app = express();
app.use("/assets", express.static(join(__dirname, "public")));
app.use("/", (req, res) => {
res.sendFile(join(__dirname, "index.html"));
});
app.listen(9000, () => console.log("listen :9000"));
What I expect is getting sample error message echoed with custom prefix ('I got the error: '
) but there's no extra output in the console, so I assume, the sample error wasn't ever caught.
So, the question is: what exactly am I missing here?