0

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?

Curr195
  • 133
  • 1
  • 5
  • `console.error()` doesn't throw an error in the traditional sense, it's just printing a string to the console with "error" formatting. – DBS Jan 08 '21 at 10:27
  • 2
    Calling `console.error` should never "*trigger certain actions*" other than logging an error to the console. – Bergi Jan 08 '21 at 10:29

3 Answers3

2

console.error is just console.log with Error: in red. It is not a window.error so the thrower needs to do

(function () {
  throw "sample error";
})();

You are able (but it is not recommended) to override the native console.error

Here I added a throw to the log

const err = console.error; // save the console.error method
console.error = str => { err(str); throw `'Throwing error':'${str}'`;  };

console.error("My Error")
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    looks like what I've been looking for was *'override `console.error`'* and you seem to address that with your snippet, thank you – Curr195 Jan 08 '21 at 10:41
2
(function () {
  console.error("sample error");
})();

This is not throwing any error it's just logging some piece of text to console.

If you want to throw an error - u should use throw statement

(function () {
  throw new Error('my error');
})();
Drag13
  • 5,859
  • 1
  • 18
  • 42
  • Because I was only going to comment, then changed my mind and now am looking to see if we can override the console.error – mplungjan Jan 08 '21 at 10:28
  • Why we can't override console error? `Object.getOwnPropertyDescriptor(console, 'error')`: `{writable: true, enumerable: true, configurable: true, value: ƒ}` – Drag13 Jan 08 '21 at 10:31
  • 1
    I [just did](https://stackoverflow.com/a/65627293/295783) – mplungjan Jan 08 '21 at 10:31
  • 1
    @mplungjan yeh, but I am not sure this code should be public :D – Drag13 Jan 08 '21 at 10:32
  • Yep, excuse me for misunderstanding – Drag13 Jan 08 '21 at 10:33
  • @Drag13 [you can hack the console a lot if you wish](https://stackoverflow.com/a/39648992/). However, I wouldn't recommend changing the behaviour of the logging unless you just want to disable, say, `console.info` or `console.debug` messages or something. – VLAZ Jan 08 '21 at 10:37
  • @VLAZ Totally agree with you. – Drag13 Jan 08 '21 at 10:38
0

As @Drag13 and @mplungjan console.log() does not throw errors so you can't catch them using window.onerror.

You have 2 choices to accomplish your goal, either change all of your console.error(error) to throw error

or override the console.error property to handle thus error logs, like this:

console.error = function (msg) {
    console.log("I got the error:", msg);
};
Dahou
  • 522
  • 4
  • 12
  • 3
    While it is technically valid, please don't use the second method, overwriting built in functions is like laying a minefield for future devs. – DBS Jan 08 '21 at 10:34