user1131553
user1131553

Reputation: 43

Google Apps Script randomly produces error message "Unexpected error occurred" after adding more code

I have written a Google Apps Script UiApp application with close to 1000 lines of code and a fair amount of handlers and callbacks. The app was working fine, but as the code grew the app suddenly got a lot of "unexpected error occurred" messages. The error messages occur as the app is loading and are intermittent. I can try the app one minute and get the error. Then refresh and the error goes away. Refresh again and the error may very well appear again. They occur in both the test and production url.

Because the error is random I can't trace it to any particular line of code, Are there processing limits that might cause this? Does Google Apps have limits on lines, widgets or handlers? If so, could those be causing these errors and are there ways to get the limits increased? Is there anything I can do to trace this problem?

Upvotes: 3

Views: 4395

Answers (2)

Trevor Iampen
Trevor Iampen

Reputation: 213

Frequently, the "unexpected error occurred" message is attributed to your handlers trying to work with elements that have not yet been created and added to your app. For example, if you try to run a handler on a panel that may very well exist in your code in a function some where, you will get an unexpected error if the panel has not yet been created.

Follow the flow of operations and functions in your code to see if something is trying to execute on an element that may exist in a function somewhere, but hasn't been added to your app yet.

Additionally, if you are trying to handle elements that have Id's through ".forTargets(app.getElementById('myElement'))", check the names of your Id's very closely. If the name is wrong, you will get that error as well.

Bug squashing can be a time consuming, hair-pulling experience! When your code starts becoming spaghetti code, these errors tend to crop up. You may have to go back and re-design and re-write to clean things up.

Upvotes: 1

Ismael Ghalimi
Ismael Ghalimi

Reputation: 3565

Yes, the engine for Google Apps Script has all kind of limitations in terms of the number of callbacks you can make, the amount of time that can be spent on executing your script, etc. Our experience is that you should keep your code as small as possible and modularize things that might take a long time. If you're using your code in combination with a spreadsheet, use the spreadsheet as a container for storing the result of intermediate steps, and break your code into multiple scripts that will call each other once they're done. It will make your code more complex, but it will work a lot more reliably. And if your code is doing things which Google Apps Script was never designed for, move the code outside, to Google App Engine or another container (especially if you need server-side JavaScript).

Upvotes: 1

Related Questions