Line and column in Javascript Error event attributes
Asked Answered
T

2

6

I've decided that there are some errors which I don't want to go to the browser error handler. But I still want to know about them. In my actual code I have a function which stores the errors in a hidden element and ajax submits them to a database. Following is a simplified version of my try block:

try
{
  newValueEvaled = eval(newValue);
}catch(err)
{
  alert("Error caught: Line " + err.lineNumber + ((err.columnNumber != undefined)?', Column:' + err.columnNumber:"") + '\n' + err.message);
}

I'd like the columnNumber too. Currently it is never there, but somehow the browser error console has access to it. Can anyone tell me how I can get access to it as well?

Toname answered 5/5, 2011 at 18:23 Comment(3)
Erm, why do you want to do this? The error going to the browser allows the native browser, WebDeveloper extension, FireBug extension to show you the same information... often with more info, and without blocking the script. .lineNumber is a Firefox only extension btw.Timepiece
@Timepiece The idea is to produce a log of all javascript errors via a simple ajax request. Errors logs are only helpful if they are specific, so including data like this is the whole point.Toname
Would be nice if this became standard developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Fransis
Q
3

I'm almost certain it's not possible to get the error column number from JavaScript running in the page. Firebug/WebKit console/IE console has access to internal browser objects that provide more information about the call stack than is available to code running inside the page.

Quetzal answered 5/5, 2011 at 18:42 Comment(2)
According to the Mozilla Docuentation (developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…) The Error object does not contain any column information. However, I still think there has to be some way to make this data available to Javascript. Maybe an addon?Toname
I imagine it's possible to write an add-on that would provide this. A quick googling suggests that it doesn't already exist. Might be a fun project?Quetzal
T
3

You can access the error line and possibly column using a custom error handler function:

function dumpErrors(error, file, line, column)
{
    alert('Error: ' + error + ', occurred in file: ' + file + ', on line: ' + line + ', at column: ' + (column || 'unknown'));
}
onerror = dumpErrors;

The «line» is available for all browsers. For the «column», it seems it's available on latest Chrome (release 30.0+), but not on Firefox (release 17, running on my Linux).

Thermionic answered 29/11, 2013 at 6:33 Comment(1)
Mobile Safari (at least in iOS 8) also supports column.Bohol

© 2022 - 2024 — McMap. All rights reserved.