Search code examples
google-apps-scriptgoogle-sheetsinternal-server-errorcustom-function

Google Sheets App Script: Internal error executing the custom function


I have a custom function that periodically will return the error: Internal error executing the custom function. The custom function execution lasts 0 (zero) seconds according to the executions page on the App Script website. enter image description here

I have contact Google multiple times about this insisting that it is a platform error, but I get the same answer every time - "Add a random delay in your function" or "Use exponential backoff". I have tried adding a random delay and this did help slightly, but the error was still there - just less occurrences. I currently have exponential backoff implemented for all App Script APIs and even with this retry logic I get the same error.

I've followed the entirety of the App Script documentation for best practices including using ranges, and even with this the error is still there.

Is anyone else experiencing this? I tried adding a Logger.log on the very first line of my custom function and this code does not trigger when I receive this error. This leads me to believe that the custom function calls just never reach the server where the code executes. This is why I believe it's a platform error.

This is the exponential backoff code:

function call_(func) {
    for (var n = 0; n < 6; n++) {
        try {
            return func()
        } catch (e) {
            Logger.log(`Retrying... ${n + 1} times exception: ${e}`)
            if (n === 6 - 1) {
                throw e
            }
            Utilities.sleep(
                Math.pow(2, n) * 1000 + Math.round(Math.random() * 1000)
            )
        }
    }
}

This is a condensed version of the custom function:

function CUSTOMFUNCTION() {
    let apiResponse = call_(() =>
            UrlFetchApp.fetch(someUrl, {
                muteHttpExceptions: true,
            })
        )

      let response = JSON.parse(call_(() => apiResponse.getContentText()))
    // do some logic with the response
    return value

}

I'm aware that http exception won't be caught and this is intentional. While testing the custom function in the development sheet that the script is attached to, I can't reproduce the error no matter what I try. The error only ever happens after the script is deployed via Google Cloud Platform (GCP). For clarity, inside GCP I use the Google Workspace Marketplace SDK to attach the script ID in order to make the add-on available to anyone to add to their Google Sheets.

If anyone has any input to help with resolving this error I would appreciate it as the Google team hasn't been very helpful over the past few months of triaging this issue.


Solution

  • The issue ended up being caused by our overall script size.

    After reaching out to google we were informed that on each custom function request Google will retrieve the entire contents of your App Script code. Not just the code/file used for custom functions. The upper limit on their download size is 5mb.

    Because of this, we changed our webpack configuration resulting in a much smaller bundle size (<1mb). Google also created a file cache to speed up this process on larger App Script projects.

    Hopefully this'll help someone else who runs into this issue.