1

I am using the following code to make a copy of a file:

var backupFolder = DriveApp.getFolderById('1vOnO5aSOSFNWtOLHuAz4Lv2lsRgBwBpS');
var file = DriveApp.getFileById('1TJ_5Khw7wcqlIfbZ_-KLOQ-DsU02FsoRFYR1A3B4qAs');
file.makeCopy(file.getName(), backupFolder);

Most of the time that works without any issue.

Some times I get the following error:

We're sorry, a server error occurred. Please wait a bit and try again

Does anyone know how I can get a more meaningful error message to understand what is the issue?

NSchorr
  • 875
  • 10
  • 13
  • 1
    You can't. This is all Google is willing to tell you. If you do the above in a loop, consider adding some Utilities.sleep to spread out the requests, just in case it helps. –  Dec 17 '17 at 19:41
  • 2
    You can wrap this in a try-catch statement and handle the error inside 'catch' - for example, by creating a time-based trigger to run this function again after some time has passed – Anton Dementiev Dec 17 '17 at 21:10
  • You could try using Google's Stackdriver logging. – NSchorr Dec 18 '17 at 01:10
  • I have thought about Google's Stackdriver logging but it only logs "Exception: We're sorry, a server error occurred. Please wait a bit and try again." – Charalampos Anargyrou Dec 19 '17 at 09:13
  • @AntonDementiev I know how to handle the exception. I am trying to find the reason for the exception. – Charalampos Anargyrou Dec 19 '17 at 09:14
  • I have also tried the API through https://developers.google.com/drive/v3/reference/files/copy but the error is even worse! `{ "error": { "errors": [ { "domain": "global", "reason": "internalError", "message": "Internal Error" } ], "code": 500, "message": "Internal Error" } }` – Charalampos Anargyrou Dec 19 '17 at 09:16
  • I don’t think the reason has anything to do with your code. Google doesn’t guarantee 100% service availability. – Anton Dementiev Dec 19 '17 at 10:53

1 Answers1

0

To build on Anton's and Nancy's answers one useful approach I have found for error logging and catching important issues, is to append the error to a google sheet for reference with a timestamp and to send me a warning email with the failure reason. This gives me an instance notice something is not right, and helps me to keep track of issues.

 function someImportantProcess(){
   var ss = SpreadsheetApp.getActiveSpreadSheet();
   var sheet = ss.getSpreadsheetByName('Sheet1');

   try {
   someImportant function(){
     Logger.log('important stuff')
   }

   } catch(e){
   sheet.appendRow([timestamp, e])
   sendEmail(e)
   }

 }

 sendEmail(e){
 var subject = '[Important a script failed]';
 var body = e;
 sendEmail(myemail@google.com, subject, body);

 }
Jason Allshorn
  • 1,625
  • 1
  • 18
  • 27