admin管理员组文章数量:1244317
I have configured PHP to send me mails whenever there is an error. I would like to do the same with Javascript.
Also given the fact that this will be client side it is open to abuse.
What are good ways to get notified by mail when JS breaks in a web application?
Update:
Just to give some perspective, i usually load several js files including libraries (most of the time jQuery).
I have configured PHP to send me mails whenever there is an error. I would like to do the same with Javascript.
Also given the fact that this will be client side it is open to abuse.
What are good ways to get notified by mail when JS breaks in a web application?
Update:
Just to give some perspective, i usually load several js files including libraries (most of the time jQuery).
Share Improve this question edited Aug 8, 2011 at 17:00 amosrivera asked Aug 8, 2011 at 16:25 amosriveraamosrivera 26.5k9 gold badges69 silver badges76 bronze badges 4- When JS breaks, isn't it be JS itself thats supposed to send u tat data? – Diff.Thinkr Commented Aug 8, 2011 at 16:27
- Not necessarily, the code could add some handler at a early point to the browser which handles errors, if it were available, but I don't know about such. – Dykam Commented Aug 8, 2011 at 16:30
- 1 One thing to note is however you handle the error, it is open to abuse. Someone could find the function call that handles it and flood your email/database. – Tom Gullen Commented Aug 8, 2011 at 16:37
- @Tom Gullen that is definitely something to take into account. I will add it to the question – amosrivera Commented Aug 8, 2011 at 16:58
5 Answers
Reset to default 9 +100You can listen to the global onError
event.
Note that you need to make sure it doesn't loop infinitely when it raises an error.
<script type="text/javascript">
var handlingError = false;
window.onerror = function() {
if(handlingError) return;
handlingError = true;
// process error
handlingError = false;
};
</script>
The code below relies on the global onError event, it does not require any external library and will work in any browser.
You should load it before any other script and make sure you have a server-side jserrorlogger.php script that picks up the error.
The code includes a very simple self-limiting mechanism: it will stop sending errors to the server after the 10th error. This es in handy if your code gets stuck in a loop generating zillions of errors.
To avoid abuse you should include a similar self-limiting mechanism in your PHP code, for example by:
- saving and updating a session variable with the error count and stop sending emails after X errors per session (while still writing them all down in your logs)
- saving and updating a global variable with the errors-per-minute and stop sending emails when the threshold is exceeded
- allowing only requests ing from authenticated users (applies only if your application requires authentication)
- you name it :)
Note that to better trace javascript errors you should wrap your relevant code in try/catch blocks and possibly use the printstacktrace function found here:
https://github./eriwen/javascript-stacktrace
<script type="text/javascript">
var globalOnError = (function() {
var logErrorCount = 0;
return function(err, url, line) {
logErrorCount++;
if (logErrorCount < 10) {
var msg = "";
if (typeof(err) === "object") {
if (err.message) {
// Extract data from webkit ErrorEvent object
url = err.filename;
line = err.lineno;
err = err.message;
} else {
// Handle strange cases where err is an object but not an ErrorEvent
buf = "";
for (var name in err) {
if (err.hasOwnProperty(name)) {
buf += name + "=" + err[name] + "&";
}
}
err = "(url encoded object): " + buf;
}
}
msg = "Unhandled exception ["+err+"] at line ["+line+"] url ["+url+"]";
var sc = document.createElement('script'); sc.type = 'text/javascript';
sc.src = 'jserrorlogger.php?msg='+encodeURIComponent(msg.substring(0, Math.min(800, msg.length)));
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(sc, s);
}
return false;
}
})();
window.onerror = globalOnError;
</script>
You would wrap your entire program in a try/catch and send caught exceptions over AJAX to the server where an email could be generated. Short of that (and I wouldn't do that) the answer is "not really."
JA Auide has the basic idea. You could also go somewhat in between, ie.:
- Write an AJAX "errorNotify" function that sends error details to the server so that they can be emailed to you.
- Wrap certain parts of your code (the chunks you expect might someday have issues) with a try/catch which invokes errorNotify in the catch block.
If you were truly concerned about having 0 errors whatsoever, you'd then be stuff with try/catching your whole app, but I think just try/catching the key blocks will give you 80% of the value for 20% of the effort.
Just a note from a person that logs JavaScript errors.
- The info that es from window.onerror is very generic. Makes debugging hard and you have no idea what caused it.
- User's plugins can also cause the issue. A very mon one in certain Firebug versions was toString().
- You want to make sure that you do not flood your server with calls, limit the amount of errors that can be sent page per page load.
- Make sure to log page url with the error call, grab any other information you can too to make your life easier to debug.
本文标签: javascriptGet notified when JS breaksStack Overflow
版权声明:本文标题:javascript - Get notified when JS breaks? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740191051a2239095.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论