引发自定义超时异常

时间:2020-02-11 16:28:07

标签: javascript google-apps-script try-catch timeoutexception

我有一个Google Apps Script网络应用程序(“ Web App”),该应用程序以用户身份执行,然后使用UrlFetchApp.fetch()通过Apps Script API调用另一个Apps Script项目(“ API Executable”)中的各个功能。像我一样执行它们(请参阅Get user info when someone runs Google Apps Script web app as me)。

此方法的局限性是UrlFetchApp.fetch()的超时时间为60s,而我的函数之一通常要花更长的时间。 API可执行函数成功完成运行,但是Web应用程序引发超时异常。我想通过运行第二个“后续”函数来处理此异常,该函数查找并返回由原始函数成功创建的Google表格的URL。但是,我需要将传递给原始函数的参数之一传递给后续函数,而且看来我无法在标准try ... catch块中执行此操作。

我的想法是抛出一个包含所需参数的异常,但是我不知道如何抛出自己的超时异常;由于Google Apps脚本是同步的,因此无法跟踪UrlFetchApp.fetch()运行期间运行了多长时间

有没有办法抛出自己的超时异常?或者,如果存在超时错误,还有其他方法可以将所需的参数传递给执行的函数吗?

我在这篇文章中也标记了Javascript,因为与Google Apps脚本有很多重叠之处,而且我认为这样做可以提高我与有答案的人联系的机会-希望可以。以下是我在网络应用中使用的函数,以调用API可执行函数(如果有帮助的话)。

编辑:基于@TheMaster的评论,我决定编写脚本,就像传递给executeAsMe()的参数被传递给catch()块一样,以查看发生了什么。我希望对opt_timeoutFunction未定义这一事实有一个例外,但奇怪的是,它似乎仅在catch()块的第一行正在运行,我不确定为什么。

function executeAsMe(functionName, paramsArray, opt_timeoutFunction, opt_timeoutParams) {
  try {
    console.log('Using Apps Script API to call function ' + functionName.toString() + ' with parameter(s) ' + paramsArray.toString());

    var url = 'https://script.googleapis.com/v1/scripts/Mq71nLXJPX95eVDFPW2DJzcB61X_XfA8E:run';

    var payload = JSON.stringify({"function": functionName, "parameters": paramsArray, "devMode": true})

    var params = {method:"POST",
                  headers: {Authorization: 'Bearer ' + getAppsScriptService().getAccessToken()},
                  payload:payload,
                  contentType:"application/json",
                  muteHttpExceptions:true};

    var results = UrlFetchApp.fetch(url, params);
    var jsonResponse = JSON.parse(results).response;
    if (jsonResponse == undefined) {
      var jsonResults = undefined;
    } else {
      var jsonResults = jsonResponse.result;
    }
  } catch(error) {
    console.log('error = ' + error); // I'm seeing this in the logs...
    console.log('error.indexOf("Timeout") = ' + error.indexOf("Timeout").toString); // ...but not this. It skips straight to the finally block
    if (error.indexOf('Timeout') > 0) { // If error is a timeout error, call follow-up function
      console.log('Using Apps Script API to call follow-up function ' + opt_timeoutFunction.toString() + ' with parameter(s) ' + paramsArray.toString());

      var url = 'https://script.googleapis.com/v1/scripts/Mq71nLXJPX95eVDFPW2DJzcB61X_XfA8E:run';

      var payload = JSON.stringify({"function": opt_timeoutFunction, "parameters": opt_timeoutParams, "devMode": true})

      var params = {method:"POST",
                    headers: {Authorization: 'Bearer ' + getAppsScriptService().getAccessToken()},
                    payload:payload,
                    contentType:"application/json",
                    muteHttpExceptions:true};

      var results = UrlFetchApp.fetch(url, params);
      var jsonResponse = JSON.parse(results).response;
      if (jsonResponse == undefined) {
        var jsonResults = undefined;
      } else {
        var jsonResults = jsonResponse.result;
      }
    }
  } finally {
    console.log('jsonResults = ' + jsonResults);
    return jsonResults;
  }

}

1 个答案:

答案 0 :(得分:0)

我最终使用了'''catch()'''块将异常抛出回客户端并在那里进行处理。

Google Apps脚本:

function executeAsMe(functionName, paramsArray) {
  try {
    console.log('Using Apps Script API to call function ' + functionName.toString() + ' with parameter(s) ' + paramsArray.toString());

    var url = 'https://script.googleapis.com/v1/scripts/Mq71nLXJPX95eVDFPW2DJzcB61X_XfA8E:run';

    var payload = JSON.stringify({"function": functionName, "parameters": paramsArray, "devMode": true})

    var params = {method:"POST",
                  headers: {Authorization: 'Bearer ' + getAppsScriptService().getAccessToken()},
                  payload:payload,
                  contentType:"application/json",
                  muteHttpExceptions:true};

    var results = UrlFetchApp.fetch(url, params);
    var jsonResponse = JSON.parse(results).response;
    if (jsonResponse == undefined) {
      var jsonResults = undefined;
    } else {
      var jsonResults = jsonResponse.result;
    }
    return jsonResults;
  } catch(error) {
    console.log('error = ' + error);
    if (error.toString().indexOf('Timeout') > 0) {
      console.log('Throwing new error');
      throw new Error('timeout');
    } else {
      throw new Error('unknown');
    }
  } finally {
  }
}

客户端Javascript(简化版):

 function createMcs() {
      var userFolder = getDataFromHtml().userFolder;    

      google.script.run
        .withSuccessHandler(createMcsSuccess)
        .withFailureHandler(createMcsFailure)
        .withUserObject(userFolder)
        .executeAsMe('createMasterCombinedSchedule', [userFolder]);
    }

    function createMcsSuccess(mcsParameter) {
      if (mcsParameter == undefined) {
        simpleErrorModal.style.display = "block"; // A generic error message
      } else {
        document.getElementById("simpleAlertHeaderDiv").innerHTML = 'Master Combined Schedule Created Successfully';
        document.getElementById("simpleAlertBodyDiv").innerHTML = 'Your Master Combined Schedule was created successfully. Click <a href="' + mcsParameter + '" target="_blank">here</a> to view.';
        simpleAlertModal.style.display = "block";
      }
    }

    function createMcsFailure(mcsError, userFolder, counter) { // The exception I threw will activate this function
      if (!counter) { // Added a counter to increment every time checkForCreatedMcs() runs so it doesn't run indefinitely
        var counter = 0;
      }
      if (mcsError.message == 'Error: timeout' && counter < 5) { // If timeout error, wait 10s and look for MCS URL
        window.setTimeout(checkForCreatedMcs(mcsError, userFolder, counter), 10000);
      } else if (mcsError.message == 'Error: timeout' && counter == 5) { // If it hasn't worked after 5 tries, show generic error message
        simpleErrorModal.style.display = "block";
      } else { // For any error that's not a timeout exception, show generic error message
        simpleErrorModal.style.display = "block"; 
      }
    }

    function checkForCreatedMcs(mcsError, userFolder, counter) {
      counter++;
      google.script.run
        .withSuccessHandler(checkForCreatedMcsSuccess)
        .withUserObject([mcsError, userFolder, counter])
        .executeAsMe('checkIfMcsExists', [userFolder]); // checkIfMcsExists() is a pre-existing function in my API Executable project I had already used elsewhere      
    }

    function checkForCreatedMcsSuccess(mcsExistsParameter, params) {
      var mcsError = params[0];
      var userFolder = params[1];
      var counter = params[2];
      if (mcsExistsParameter == undefined) { // If function returns undefined, show generic error message
        simpleErrorModal.style.display = "block";
      } else if (mcsExistsParameter == false) { // If function returns false, wait 10s and try again
        createMcsFailure(mcsError, userFolder, counter);
      } else { // If function returns URL, show success modal with link
        document.getElementById("simpleAlertHeaderDiv").innerHTML = 'Master Combined Schedule Created Successfully';
        document.getElementById("simpleAlertBodyDiv").innerHTML = 'Your Master Combined Schedule was created successfully. Click <a href="' + mcsExistsParameter + '" target="_blank">here</a> to view.';
        simpleAlertModal.style.display = "block";
      }
    }

我确信必须有一种更简洁/更简单的方法来做到这一点,但这是可行的!