admin管理员组文章数量:1134599
I'm trying to find or figure out a way to display in an alert box all of the unhandled javascript exceptions in an application. I'd want all of this to be done on the client side, without using any server side code. I'm using MVC3 as an environment.
I've been researching for the last few days and haven't found exactly what I'm looking for.
I found 2 ways below that seem like they're almost what I'm looking for, except these ways are set up so that you have to pass a function name into a custom method to print the stack trace of all unhandled exceptions within that one specific function. I'm looking for a way to not have to manually pass a function name to a custom method that prints the stack trace of all of the unhandled exceptions. I'd want these custom method to just 'listen' for all unhandled exceptions within the whole application.
/
Also something similar to the previous link:
Here's the basic code from the 2nd link above that prints the stack trace of a specified javascript function:
instrumentFunction: function (context, functionName, callback) {
context = context || window;
var original = context[functionName];
context[functionName] = function instrumented() {
callback.call(this, printStackTrace().slice(4));
return context[functionName]._instrumented.apply(this, arguments);
};
context[functionName]._instrumented = original;
}
function printStackTrace(options) {
options = options || {
guess: true
};
var ex = options.e || null,
guess = !! options.guess;
var p = new printStackTrace.implementation(),
result = p.run(ex);
return (guess) ? p.guessAnonymousFunctions(result) : result;
}
So, to sum this up, do you all know of any way to have some sort of 'listener' to listen for all javascript unhandled exceptions and then print them to the screen in an alert box?
Thanks! Jason
I'm trying to find or figure out a way to display in an alert box all of the unhandled javascript exceptions in an application. I'd want all of this to be done on the client side, without using any server side code. I'm using MVC3 as an environment.
I've been researching for the last few days and haven't found exactly what I'm looking for.
I found 2 ways below that seem like they're almost what I'm looking for, except these ways are set up so that you have to pass a function name into a custom method to print the stack trace of all unhandled exceptions within that one specific function. I'm looking for a way to not have to manually pass a function name to a custom method that prints the stack trace of all of the unhandled exceptions. I'd want these custom method to just 'listen' for all unhandled exceptions within the whole application.
http://eriwen.com/javascript/js-stack-trace/
Also something similar to the previous link:
https://github.com/eriwen/javascript-stacktrace
Here's the basic code from the 2nd link above that prints the stack trace of a specified javascript function:
instrumentFunction: function (context, functionName, callback) {
context = context || window;
var original = context[functionName];
context[functionName] = function instrumented() {
callback.call(this, printStackTrace().slice(4));
return context[functionName]._instrumented.apply(this, arguments);
};
context[functionName]._instrumented = original;
}
function printStackTrace(options) {
options = options || {
guess: true
};
var ex = options.e || null,
guess = !! options.guess;
var p = new printStackTrace.implementation(),
result = p.run(ex);
return (guess) ? p.guessAnonymousFunctions(result) : result;
}
So, to sum this up, do you all know of any way to have some sort of 'listener' to listen for all javascript unhandled exceptions and then print them to the screen in an alert box?
Thanks! Jason
Share Improve this question edited Mar 25, 2014 at 8:56 jgillich 76k7 gold badges60 silver badges88 bronze badges asked Sep 24, 2012 at 19:33 jre247jre247 1,8875 gold badges22 silver badges28 bronze badges 3- one thing... wy would you use an alert box?!? you could use console.log(error_message) to.... – Mathlight Commented Sep 24, 2012 at 19:34
- 1 I think this question Catch all JS errors will help you out – Pilgerstorfer Franz Commented Sep 24, 2012 at 19:38
- You probably should have a look at this question: stackoverflow.com/questions/205688/… – Mark Commented Sep 24, 2012 at 19:38
5 Answers
Reset to default 162You can do this by using window.onerror method.
window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
alert("Error occured: " + errorMsg);//or any message
return false;
}
In addition to using one of these
window.onerror = function (message, file, line, col, error) {
alert("Error occurred: " + error.message);
return false;
};
window.addEventListener("error", function (e) {
alert("Error occurred: " + e.error.message);
return false;
})
You can also catch all the errors fired inside a promise callback (.then()
) listening for unhandledrejection
event
window.addEventListener('unhandledrejection', function (e) {
alert("Error occurred: " + e.reason.message);
})
You can either use window.onerror
, or (amazingly!) bind to the 'error' event properly:
window.onerror = function (message, file, line, col, error) {
alert("Error occurred: " + error.message);
return false;
};
window.addEventListener("error", function (e) {
alert("Error occurred: " + e.error.message);
return false;
})
If you want to track JavaScript errors, you can try Atatus. I work at Atatus.
You have two event listeners that can be used for this: unhandledrejection
and error
.
The first one unhandledrejection:
The unhandledrejection event is sent to the global scope of a script when a JavaScript Promise that has no rejection handler is rejected; typically, this is the window, but may also be a Worker.
To have a global error handler for promises you can listen for this events:
// event is of type PromiseRejectionEvent
window.addEventListener("unhandledrejection", (event) => {
event.preventDefault(); // This will not print the error in the console });
console.log("Unhandled promise rejection");
console.log(event.reason);
});
In the usage notes, it mentions:
Allowing the unhandledrejection event to bubble will eventually result in an error message being output to the console. You can prevent this by calling preventDefault() on the PromiseRejectionEvent; see Preventing default handling below for an example.
Thus, if you don't want to see the message being logged by the environment, event.preventDefault()
can be used.
The second one error:
The error event is fired on a Window object when a resource failed to load or couldn't be used — for example if a script has an execution error.
To have a global error handler for the other errors you can listen for this events:
window.addEventListener("error", (event) => {
event.preventDefault(); // This will not print the error in the console
console.log("Unhandled error encountered");
console.log(event.message);
});
Check out http://log4javascript.org it is based on Log4J. If most of your code is wrapped in try/catch statements to handle exceptions you can make use of this library as a common interface for sending output to an always available "dialog box" or logging window that your end user could see. You could even have a button that performs a window.print() to print the contents of the dialog box to the printer or PDF. Good luck.
本文标签: Catching all javascript unhandled exceptionsStack Overflow
版权声明:本文标题:Catching all javascript unhandled exceptions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736805287a1953659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论