16

I'm trying to understand why Firefox (I'm using 15 but it's the same even in nightly) is not behaving like WebKit when trying to access error event information.

This one works everywhere:

window.onerror = function(message, lineno, filename) { }

But of course I don't want to use this.

The right thing to do is:

window.addEventListener('error', function(e) { 
  console.log(e.message);
}, false);

Unfortunately this one only works in WebKit. In Firefox the handler is called, but the e event is almost empty: no message, no line number, no filename properties.

The very minimal test is here: http://jsbin.com/efexiw/1/edit

I don't think this is a bug, though... so the question is: how do I get the error details in recent Firefox?

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
Claudio
  • 5,740
  • 5
  • 33
  • 40

1 Answers1

25

The HTML5 specification requires that a parse failure causes the browser to:

...report the error for script, with the problematic position (line number and column number), using the global object... as the target.

Where "report the error" includes the steps

  1. Let message be a user-agent-defined string describing the error in a helpful manner.

...

  1. Let event be a new trusted ErrorEvent object that does not bubble but is cancelable, and which has the event name error.

  2. Initialize event's message attribute to message.

...

  1. Dispatch event at target.

Thus, any HTML5-compliant browser will report parse-time error events on window, which include a message attribute set to a "user-agent-defined string describing the error in a helpful manner." Any browser version that fails to do this is not yet HTML5 compliant in this regard.


Previously (at the time this question was written), window.onerror gave information that was not provided by window.addEventListener("error"). If you must use an old version of Firefox, you can safely use window.onerror:

// Example 1:

// Prevent error dialogs from displaying -which is the window's normal
// behavior- by overriding the default event handler for error events that
// go to the window.
window.onerror = null;

// Example 2:

var gOldOnError = window.onerror;
// Override previous handler.
window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
  if (gOldOnError)
    // Call previous handler.
    return gOldOnError(errorMsg, url, lineNumber);

  // Just let default handler run.
  return false;
}
Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • I accept the answer because at least I know how to handle the situation. Too bad I did not understand why Firefox _seems_ to have removed this "feature" (for what I've been reading in my searches are old examples which there are supposed to work in FF and other browsers). – Claudio Oct 05 '12 at 14:14
  • In FF 38, at least, probably much sooner, the error event provided to addEventListener has at least message, filename and lineno properties... maybe more. I'm presently on a search for more, and for any standard that defines particulars. – Victoria Mar 04 '15 at 21:50
  • Details of error event are lacking, but [MDN](https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent) shows .colno available, and experimentation says .error.stack is available in FF38. – Victoria Mar 04 '15 at 22:28