3

I've created a google app script that runs on a Google Sheets file. Every day at ~9am a trigger runs on of my functions.

Every few days (I don't know why) but I get an

Execution Error: Service invoked too many times for one day: urlfetch

I've given up trying to solve that issue, but it would be great if at the end of my function I could include some code to check if there was an execution error and if so run another function e.g.

myfunction_9am() {
    <try run some code>
    if (execution Status = "Failed") {      <<this is the bit I don't know how to code
        myfunction_error();
    }
}
1
  • 1
    Use try{ //Some code }catch(e) {//Code to run only if there is an error} The letter e in the parenthesis is an "exception object" that contains a JavaScript error message, and the "stack." If the catch block runs code, then the execution failed. You don't need to test for whether it failed or not. To get the error message use e.message and to get the stack use e.stack. Commented Sep 26, 2019 at 0:03

1 Answer 1

2

Error handling in Apps Script can be handled by both JavaScript and Apps Script troubleshooting tools.

function myFunction() {
try{
  //Some code 
}catch(e) {
  //Code to run only if there is an error
  console.error("Error: " + e + "Stack: " + e.stack);//Send to StackDriver
  myErrorHandlingFunction(e);
} 
}

Error handling function:

function myErrorHandlingFunction(e) {
  var body,errMsg, recipient,stack;

  if (!e) {
    return;
  }

  errMsg = e.message;
  stack = e.stack;

  recipient = Session.getEffectiveUser().getEmail();
  subject = "Error in Code";

  body = 'The error is: ' + errMsg + "\n\nStack:" + stack;
  GmailApp.sendEmail(recipient, subject, body);
}

The letter e in the parenthesis is an "exception object" that contains a JavaScript error message, and the "stack." If the catch block runs code, then the execution failed. You don't need to test for whether it failed or not. To get the error message use e.message and to get the stack use e.stack.

You may wish to send error information to Apps Script StackDriver service and/or handle the error through your own code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.