How can we listen for errors that do not trigger window.onerror?
2017-04-12 21:57
423 查看
原文: http://stackoverflow.com/questions/19141195/how-can-we-listen-for-errors-that-do-not-trigger-window-onerror
Could you please provide a small code example to show how we can listen for such errors? Can we listen for SyntaxError too?
For a small code example to show how we can listen for such errors:
running this example in your browser will pop up an alert message similar to this:
JavaScript error: SyntaxError: missing ) after argument list on line 26 for page_url
In the above example:
If you run the same example by putting
JavaScript error: TypeError: document.getElementById(...) is null on line 25 for page_url
window.onerror acts something like a global try/catch block, allowing you to gracefully handle(even with server logging) uncaught exceptions you didn’t expect to see:
uncaught exceptions
some more compile error
But two major issues described here nicely:
Unlike a local try/catch block, the window.onerror handler doesn’t have direct access to the exception object, and is executed in the global context rather than locally where the error occurred. That means that developers don’t have access to a call stack, and can’t build a call stack themselves by walking up the chain of a method’s callers.
Browsers go to great lengths to sanitize the data provided to the handler in order to prevent unintentional data leakage from cross-origin scripts. If you host your JavaScript on a CDN (as you ought), you’ll get “Script error.”, “”, and 0 in the above handler. That’s not particularly helpful.
window.onerroris triggered whether it was a syntax or runtime error. This page from quirksmode has lists of what error events it will and will not catch.
Could you please provide a small code example to show how we can listen for such errors? Can we listen for SyntaxError too?
For a small code example to show how we can listen for such errors:
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> window.onerror = function (errorMsg, url, lineNumber) { alert(errorMsg + lineNumber); // alert("This is a stack trace! Wow! --> %s", error.stack); }; </script> </head> <body> <script type="text/javascript"> //var x=document.getElementById("demo").value; //uncomment and run to see document.write('careless to close the parentheses?'; // ')' is not given </script> </body> </html>
running this example in your browser will pop up an alert message similar to this:
JavaScript error: SyntaxError: missing ) after argument list on line 26 for page_url
In the above example:
window.onerror = function(message, url, linenumber), the arguments are:
message: the error message (DOMString)
url: the URL of the file containing the error (DOMString)
linenumber: the line number where the error occurred (unsigned long)
If you run the same example by putting
var x=document.getElementById("demo").value;instead of the code with syntax error(as i have shown in the example), it will also be caught by the
window.onerror()function and will show an alert message similar to this:
JavaScript error: TypeError: document.getElementById(...) is null on line 25 for page_url
window.onerror acts something like a global try/catch block, allowing you to gracefully handle(even with server logging) uncaught exceptions you didn’t expect to see:
uncaught exceptions
throw "some messages"
call_something_undefined();
cross_origin_iframe.contentWindow.document;, a security exception
some more compile error
<script>{</script>
<script>for(;)</script>
<script>"oops</script>
setTimeout("{", 10);, it will attempt to compile the first argument as a script
But two major issues described here nicely:
Unlike a local try/catch block, the window.onerror handler doesn’t have direct access to the exception object, and is executed in the global context rather than locally where the error occurred. That means that developers don’t have access to a call stack, and can’t build a call stack themselves by walking up the chain of a method’s callers.
Browsers go to great lengths to sanitize the data provided to the handler in order to prevent unintentional data leakage from cross-origin scripts. If you host your JavaScript on a CDN (as you ought), you’ll get “Script error.”, “”, and 0 in the above handler. That’s not particularly helpful.
shareedit | edited Oct 6 '13 at 11:04 | answered Oct 2 '13 at 16:03 Sage 12k11629 |
相关文章推荐
- How do you define a copy constructor or assignment operator for a class that contains a pointer to a (abstract) base class?
- vector iterator not incrementable For information on how your program can cause an an assertion Failure, see the Visual c + + documentation on asserts
- How can we identify the weights of different models for ensemble?
- [导入]How can I update my user interface from a thread that did not create it?
- Unity3d for android 访问UI控件报错 Can\'t create handler inside thread that has not called Looper.prepare()
- application使用@符合问题:'@' that cannot start any token. (Do not use @ for indentation)
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12
- How do I listen for a constrained JavaBean prop...
- How can you troubleshoot to try to resolve common HTTP errors that occur when working with Lotus Dom
- Comics: do we know that we are not doing research in the wrong way?
- How can we decide whether MPI, Pthreads or OpenMP is best for our application?
- How to create MFC applications that do not have a menu bar in Visual C++(MFC单文档和多文档程序中去掉菜单栏)(转)
- file - How do I remove/delete a folder that is not empty with Python? - Stack Overflow
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- How do I open an editor on something that is not a file?
- In how many ways can we select some of these coins so that they are X yen in total?
- bug fix: openstack can not run swift for pyeclib and liberasurecode do not match
- brew install失败:提示We do not provide support for this pre-release version.
- How do I open an editor on something that is not a file?
- [MongoDB]How do I query for fields that have null values?