When running a google apps script from a google spreadsheet, if one of the google apis is used incorrectly a red "butterbar" error is shown at the top of the spreadsheet. This message usually contains info that is useful to the script developer (an error message from a google api, e.g. "The coordinates or dimensions of the range are invalid.") but not necessarily to the spreadsheet user (a real world translation of what they can do to resolve it).
I searched through the UiApp api documentation but didn't see a way of customizing this message. Is it possible to throw your own error message?
You can try to have a look to the Base class that gives you the possibility to add alert box or other dialogs to interact with the user of your application if it is bound to a Spreadsheet (for example msgBox()). For a DocumentApp use the Ui class to interact with its interface.
I never tried but if you use a try...catch structure with a throw statement it can also work.
Cheers
Nicolas
As with any javascript, you can use:
There are numerous examples of this in use, even if the questions aren't exactly yours.
My personal opinion is that it's best if you check the input to your function and throw errors on that (like this answer), rather than catching the errors from service calls. An appropriate time to use try..catch would be when you have no practical way to validate parameters, as in this answer.
Here is the best way to pass data from your script into your error message. First setup a global variable object to store your data that you need for the error handling. Then store data to this object in each function's
try{}
section. Finally incatch(e)
call anerrHandler
function that will pass the error data object. See the following code:code.gs
.