Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception.lineNumber returns "undefined" in google chrome and internet explorer

I need to get fileName, Message, LineNumber etc from a javascript exception. I tried the following code.

try {
  alertt("dddd");
} catch (e) {
  console.log("ExceptionType: "+ e.name);
  console.log("Message: "+ e.message);
  console.log("Line No: "+ e.lineNumber);
}

I got the following result in Mozilla Firefox

ExceptionType: ReferenceError
Message: alertt is not defined
Line No: 4

But the same code gave the following result in Google Chrome, Internet Explorer

ExceptionType: ReferenceError
Message: alertt is not defined
Line No: undefined

It is not giving the Line Number. How to solve this issue? Is there any another method for getting Line number?

I tried e.stack It returns the stack trace as string. It gave me the following output in Google Chrome

 ReferenceError: alertt is not defined
    at message (http://localhost/ems-test/js/test.js:4:4)
    at HTMLDocument.<anonymous> (http://localhost/ems-test/js/test.js:14:2)
    at c (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26036)
    at Object.p.fireWith [as resolveWith] (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26840)
    at Function.x.extend.ready (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:3305)
    at HTMLDocument.q (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:717) 

and firefox gave this result

message@http://localhost/ems-test/js/test.js:4
@http://localhost/ems-test/js/test.js:14
x.Callbacks/c@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
.ready@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
q@http://localhost/ems-test/js/jquery-1.10.2.min.js:4

Both are string type result. Not an object. So it needs to extract the line number from this huge string. But the problem is both result are not same. One shows the line number in first line and another one shows it in second line. So it will be difficult to extract line number from this huge string.

Is there any method to get the stack trace as an object?

like image 860
Sajith Avatar asked Oct 20 '22 21:10

Sajith


2 Answers

window.onerror = function (msg, url, line) {
   alert("Message : " + msg );
   alert("url : " + url );
   alert("Line number : " + line );
}

Hope this might help you. check on this link: http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=javascript_40

like image 50
robieee Avatar answered Oct 31 '22 00:10

robieee


I don't have IE on my machine, so I can't speak for that; but in Chrome, you might be able to get what you need by looking at the e.stack property and parsing it out. You can see the options available to you if you do a console.dir(e) within your catch block.

like image 33
Mike S. Avatar answered Oct 31 '22 01:10

Mike S.