Throw custom timeout exception

江枫思渺然 提交于 2020-04-30 11:44:25

问题


I have a Google Apps Script web app ("Web App") that executes as the user, then calls individual functions from another Apps Script project ("API Executable") via the Apps Script API using UrlFetchApp.fetch() and executes them as me (see Get user info when someone runs Google Apps Script web app as me).

A limitation of this method is that UrlFetchApp.fetch() has a 60s timeout, and one of my functions often takes longer than this. The API Executable function finishes running successfully, but the web app throws a timeout exception. I would like to handle this exception by running a second "follow-up" function that finds and returns the URL of the Google Sheet successfully created by the original function. However, I'll need to pass the follow-up function one of the parameters passed to the original function, and it appears I can't do this within a standard try...catch block.

My idea was to throw an exception that contains the needed parameter, but I can't figure out how to throw my own timeout exception; since Google Apps Script is synchronous, there's no way to track how long UrlFetchApp.fetch() has been running while it's running.

Is there a way to throw your own timeout exception? Or, is there another way I can pass the needed parameter to a function that executes if there's a timeout error?

I tagged Javascript in this post as well since there's a lot of overlap with Google Apps Script and I figured it would improve my chance of connecting with someone who has an answer--hope that's okay. Below is the function I'm using in my web app to call my API Executable functions, in case that's helpful.

EDIT: Based on @TheMaster's comment, I decided to write the script as though parameters passed to executeAsMe() WERE being passed to the catch() block, to see what happened. I expected an exception regarding the fact the opt_timeoutFunction was undefined, but strangely it looks like only the first line of the catch() block is even running, and I'm not sure why.

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:


I ended up using the '''catch()''' block to throw an exception back to the client side and handle it there.

Google Apps Script:

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 {
  }
}

Client-side Javascript (a simplified version):

 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";
      }
    }

I am sure there has to be a tidier/less complex way to do this, but this worked!



来源:https://stackoverflow.com/questions/60173391/throw-custom-timeout-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!