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?

Luke
  • 435
  • 4
  • 12
  • 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](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error) is a Firefox only extension btw. – Rudu May 05 '11 at 18:34
  • @Rudu 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. – Luke May 26 '11 at 21:50
  • Would be nice if this became standard https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/columnNumber – Wesley Sep 29 '20 at 16:50

2 Answers2

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).

Yvan
  • 2,539
  • 26
  • 28
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.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • According to the Mozilla Docuentation (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error) 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? – Luke May 05 '11 at 22:06
  • 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? – Tim Down May 05 '11 at 23:19