0

I am writing a google web app which uses the Admin Directory a lot. However I was wondering how the error handling should be done since I do not get a proper error object back when a request to the api fails.

Example: I want to check if a custom schema exists and if not I want to do something else:

 try{
   var resp =  AdminDirectory.Schemas.get("129898rgv", "someCustomSchema");
  }catch(err){
     // if schema does not exist do this
      schemaNotExistFunction();
      Logger.log(err);
   }

Unfortunately I do not even get the http status code back from the err. Is there another way to handle errors in Google Apps Script?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
zlZimon
  • 2,334
  • 4
  • 21
  • 51
  • 1
    What do you get in the Logger.log message? Have you tried with `console.log`? – Jescanellas Jun 26 '20 at 11:09
  • I get the an error message but no error code or anything useful. – zlZimon Jun 26 '20 at 12:16
  • @zlZimon - but you get *something*, right? Please include the error message in the question. You should not get an HTTP status code back as the advanced service wraps around the REST API (although, an error should be thrown). If you need that much control over the process, I would suggest using the API directly and skip the advanced service – Oleg Valter is with Ukraine Jun 26 '20 at 13:26
  • @Rubén - I haven't got time to add it yet :) But will soon - are you against it overall or just not sure (to not clutter the question we can discuss on meta I think) and it can be alleviated via comprehensive tag wiki? I thought that it would be useful to have a tageted tag as they are their own phenomenon and it would be nice to be able to discern between REST API questions and advanced services (this question as an example talks about Admin SDK in context of corresponding advanced service). – Oleg Valter is with Ukraine Jun 26 '20 at 16:12
  • 1
    @Rubén - no worries, I mentioned just in case my response accidentally spill into a conversation here (not necessarily us). Chatting aside, zlZimon just as a note - no, there is no other way to handle them - error handling is the same as in JavaScript - unless you use the API directly, then you will get status code, response body, etc. – Oleg Valter is with Ukraine Jun 26 '20 at 20:16
  • @OlegValter thanks, well I was hoping to at least get a http status code back – zlZimon Jun 29 '20 at 07:06
  • Alas! In your case, I think it would be simpler to use the API directly - you can create your own wrapper whose methods return both ststus codes and contents. After all, since `getOAuthToken` handling authorization became significantly easier – Oleg Valter is with Ukraine Jun 29 '20 at 09:20

1 Answers1

3

Instead of

Logger.log(error) 

use

Logger.log('%s, %s',error.message, error.stack);

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error for a complete list of Error instance properties

The above because Logger.log parses the parameter as string. When you pass an error object error.name is logged, by the other hand by using

Example

Running the following code in an standalone project using the new runtime (V8) will log a couple of messages.

function myFunction() {
  try{
    SpreadsheetApp.getUi();
  } catch (error) {
    Logger.log(error);
    Logger.log('%s, %s', error.message, error.stack);
  }
}

Another alternative is to use console.log instead of Logger.log

function myFunction() {
  try{
    SpreadsheetApp.getUi();
  } catch (error) {
    console.log(error);
    console.log('%s, %s', error.message, error.stack);
  }
}

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • thanks, however that does not make it easier to actually handle different errors in the app script. – zlZimon Jun 29 '20 at 07:09