Skip to content

Instantly share code, notes, and snippets.

@purplecabbage
Last active August 29, 2015 13:57
Show Gist options
  • Save purplecabbage/4571cc93a913eb8e593e to your computer and use it in GitHub Desktop.
Save purplecabbage/4571cc93a913eb8e593e to your computer and use it in GitHub Desktop.
Get js stack trace from exception
(function () {
var locStr = window.location.toString();
// we will want to clean up the full path for iOS ...
var splitStr = locStr.substring("file://", locStr.lastIndexOf("/") - 1);
console.trace = function () {
try {
throw new Error();
} catch (e) {
// skip the first 2 items, they will be the error we just threw and info on this very function
if (e.stack) {
var cleanStack = e.stack.split("\n").slice(2);
for (var n = 0; n < cleanStack.length; n++) {
cleanStack[n] = cleanStack[n].split(splitStr).join("");
}
this.log(cleanStack.join("\n"));
}
}
}
})();
var testFunk = function () {
console.trace();
}
function callMe() {
testFunk();
}
document.addEventListener('deviceready', function iAmFunky(){
callMe();
}, false);
@purplecabbage
Copy link
Author

WP8 output:

stack =
at testFunk (x-wmapp0:www/js/index.js:30:5)
at callMe (x-wmapp0:www/js/index.js:35:5)
at iAmFunky (x-wmapp0:www/js/index.js:39:5)
at fire (x-wmapp0:www/cordova.js:750:13)
at Anonymous function (x-wmapp0:www/cordova.js:223:21)

@shazron
Copy link

shazron commented Mar 13, 2014

There is already a console.trace native function in iOS. Also, if you re-define console.trace, it should be in your deviceready handler since the console plugin probably will clobber your re-definition.

@shazron
Copy link

shazron commented Mar 13, 2014

iOS 7.1 output:

stack =
callMe@file:///Users/shaz/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F40363A6-5AFE-4E19-8125-A1A4C928595D/HelloCordova.app/www/js/index.js:12:13
iAmFunky@file:///Users/shaz/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F40363A6-5AFE-4E19-8125-A1A4C928595D/HelloCordova.app/www/js/index.js:26:33
fire@file:///Users/shaz/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F40363A6-5AFE-4E19-8125-A1A4C928595D/HelloCordova.app/www/cordova.js:750:28
file:///Users/shaz/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/F40363A6-5AFE-4E19-8125-A1A4C928595D/HelloCordova.app/www/cordova.js:223:53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment