Last active
October 9, 2016 23:50
-
-
Save zzgael/c7ef1286b3471898ffdc794a0143b722 to your computer and use it in GitHub Desktop.
AdWords Script error log with stacktrace for your distant eval'd scripts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Depends on StackFrame and ErrorStackParser ( https://github.com/stacktracejs/stacktrace.js ) | |
| This custom Error function will bring a nice stacktrace to your logs. | |
| It can also notify by email when not in preview mode ( optional ) | |
| As this works with eval'd scripts, you can just package | |
| the dependencies plus your script and use this generic piece in every script: | |
| -- USAGE IN YOUR SCRIPT -- | |
| function main() { | |
| var src = UrlFetchApp.fetch("http://domain.com/script.js?"+(new Date/1E3|0)).getContentText(); | |
| try { | |
| eval(src); | |
| } catch (e) { | |
| throw new StackedError(e, src, [email protected]); | |
| } | |
| } | |
| */ | |
| function StackedError(e, src, notifyEmail) { | |
| var lines = src.split("\n"); | |
| var getLine = function (number) { | |
| return ':' + number + " >> " + lines[number].trim().substr(0, 150); | |
| }; | |
| var accountId = AdWordsApp.currentAccount().getCustomerId(); | |
| var stringifiedStack = ErrorStackParser.parse(e).map(function (sf) { | |
| // This padding seems to work out for me. Double check it though | |
| var padding = 1; | |
| var lineNumber = sf.lineNumber - padding; | |
| return (sf.functionName ? sf.functionName + sf.fileName : sf.fileName) + | |
| (lines[lineNumber] ? getLine(lineNumber) : '') | |
| }).join('\n'); | |
| if (!AdWordsApp.getExecutionInfo().isPreview() && notifyEmail) { | |
| MailApp.sendEmail( | |
| notifyEmail, | |
| accountId + ' AdWords Script ' + e.message, | |
| e.message+"\n"+stringifiedStack, | |
| {noReply: true} | |
| ); | |
| Logger.log("Debug : Email sent to " + recipient); | |
| } | |
| this.message = e.message; | |
| this.name = e.name; | |
| Logger.log(stringifiedStack); | |
| } | |
| StackedError.prototype = Object.create(Error.prototype); | |
| StackedError.prototype.constructor = StackedError; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment