Friday, May 17, 2024
74
rated 0 times [  75] [ 1]  / answers: 1 / hits: 23377  / 13 Years ago, wed, august 17, 2011, 12:00:00

Javascript has this great callback window.onerror. It's quite convenient to track any error. However, it calls with the error name, the file name and the line. It's certainly not as rich as getting the actual error object from a try...catch statement.
The actual error object contains a lot more data, so I am trying to get that. Unfortunately, try...catch statement do not work fine when you start having async code.



Is there a way to combine and get the best of both worlds? I initially looked for a way to get the last error triggered within an onerror block, but it looks like JS doesn't store that.



Any clue?


More From » error-handling

 Answers
11

If you're referring to stack trace of the error object, then AFAIK, this is not possible.



Simple reason being that a stack trace is related to an execution context in which runtime exceptions (handled with try...catch...finally) were created or thrown (with new Error() or throw).



Whereas when window.onerror is invoked, it is called within a different context.



You can get some mileage by inspecting window.event (not available on FF) in your onerror handler.


[#90560] Tuesday, August 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
acaciac

Total Points: 317
Total Questions: 117
Total Answers: 128

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
;