Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass back a custom error message from google apps scripts?

Tags:

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?

like image 902
Ralf Haring Avatar asked Jun 18 '13 22:06

Ralf Haring


People also ask

What does return do in Google Apps Script?

The return keyword tells Apps Script that you're about to return a value from the function. A function can only return a maximum of one value but you can have functions that don't return any value. If the function accepts multiple input values, use a comma to separate them.

How do I debug a Google App script?

To run the script in debug mode, at the top of the editor click Debug. Before the script runs the line with the breakpoint it pauses and displays a table of debug information. You can use this table to inspect data like the values of parameters and the information stored in objects.


2 Answers

As with any javascript, you can use:

try {   ... } catch (error) {   throw new Error( "More meaningful error." ); } 

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.

like image 163
Mogsdad Avatar answered Sep 18 '22 19:09

Mogsdad


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 in catch(e) call an errHandler function that will pass the error data object. See the following code: code.gs.

var onErrObj = {} function myFunction(){   try{   // function code goes here;   // add more elements to onErrObj as desired;  }catch(e){       onErrObj['data1'] = 'someData';       onErrObj['data'] = 'some Other Data';       errHandler(e,'myFunction');  }   function errHandler(e,strFunc){       var message = e.message+'\n in file: '+e.fileName+' on line: '+e.lineNumber;       var sendto = '[email protected]';       var subject = 'My App encountered an error occured in '+strFunc;       var errProps = JSON.stringify(this.onError);       message = subject+'\n'+message+'\n onError: '+errProps;       GmailApp.sendEmail(sendto, subject, message);  } 
like image 33
user3219703 Avatar answered Sep 19 '22 19:09

user3219703