Error handling in Google Apps Script doPost

5,529 views
Skip to first unread message

Irfan Alam

unread,
Aug 8, 2019, 7:38:09 AM8/8/19
to Google Apps Script Community

I have a doPost script which is called from outside. This doPost function calls other functions below it for processing. Sometime some of the functions fails and it do not return proper output and because of that outer calling function fails.
When I come and check Executions there is no error. When I run it from Editor, it show me the error.

How to log error to a sheet or send the error via email when the doPost script is called from outside? Any idea?

Alan Wells

unread,
Aug 8, 2019, 9:26:36 AM8/8/19
to Google Apps Script Community
You can start with a JavaScript try/catch. 

function doPost(e) {
try{
  //The code

}catch(e){
  //To get the error message use e.message
  //To get the stack use e.stack - includes the line that the error occurred on

  myErrorHandlingFunction(e);
}

function myErrorHandlingFunction(e) {
  //Process the error

}
}

There are sometimes when I like to use Stackdriver to log errors in order to avoid giving permission to sending an email or access to Google Sheets, or when the code is running from a simple trigger which prohibits an external request.

If the code were running from someone else's account, and you didn't want to ask them for permission to send an email or give access to your spreadsheet, then you could make a UrlFetchApp.fetch(my web app url for error processing) call to your account, and then the web app running from your account can access your account.
Reply all
Reply to author
Forward
0 new messages