Catch all errors

4,188 views
Skip to first unread message

Aaron Weiner

unread,
May 22, 2017, 2:55:13 PM5/22/17
to AdWords Scripts Forum
Hello, 

I have a semi-complex script that I have created and it has worked perfectly for months. 

It's using the executeInParallel function to process multiple accounts at the same time. 

Recently, I noticed that it was throwing an error when I reviewed the logs. 

I have since fixed the problem which was a mistake on my part.

That said, what's the best way for setting up a catch all type of error reporting across this entire script?

Thanks  

Vincent Racaza (AdWords Scripts Team)

unread,
May 23, 2017, 1:36:17 AM5/23/17
to AdWords Scripts Forum
Hi Aaron,

You could set-up a try-catch logic in your script and have the script email you if any errors have been encountered during the execution. You could also write the error information to a spreadsheet doc using the SpreadsheetApp class and then email a link to this document.

Below is a sample code snippet:
function main() {
   
try {
   
//Insert your script logic here
 
} catch(e) {
    //Catch any error here. Example below is just sending an email with the error.
    MailApp.sendEmail('INSERT_EMAIL_ADDRESS_HERE',
                   
'Sample Script - Error',
                    e
);
 
}
}

Let me know if you have further clarifications.

Thanks,
Vincent Racaza
AdWords Scripts Team

Aaron Weiner

unread,
May 23, 2017, 6:26:20 PM5/23/17
to AdWords Scripts Forum
Hello Vincent,


Would I need the try and catch in the mainprocessClientAccount, and afterProcessAllClientAccounts functions?

Or would adding it to just the main function be sufficient? 

Thanks and I look forward to your reply.

Aaron

Vincent Racaza (AdWords Scripts Team)

unread,
May 23, 2017, 11:46:25 PM5/23/17
to AdWords Scripts Forum
Hi Aaron,

The scenario for catching errors in an MCC level script which runs in parallel is different. Since the methods in executeInParallel are considered independent methods, then you need to put a try-catch logic in main(), processClientAccount() and afterProcessAllClientAccounts() functions. However, you can have a global variable which contains all the exception messages if there are any errors on these functions. And then, on the last part of the script's execution (before the main() function ends), you can send an email if the global variable is not empty.

Aaron Weiner

unread,
May 24, 2017, 6:31:38 PM5/24/17
to AdWords Scripts Forum
Thank you very much. 

Aaron
Reply all
Reply to author
Forward
0 new messages