33

I have received 341 error notification emails for the below error

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

The notification email only tells about the name of function in which the issue is occurring.

The script is not running now. Any try to run any function in the script gave the above error. Also my custom menu that i have added into the Spreadsheet using the script is gone. I found no way to debug the script as every action on the script giving me the above error.


Below is the function in which error is coming

function crunchEmails() {
  var sheet   = SpreadsheetApp.getActiveSheet();
  var gLabel  = sheet.getRange("gmailLabel").getValues();
   
   
  // check the current SpreadSheet for limits and create a 
  // new SpreadSheet if limits are approaching
  checkSpreadSheet();
  
  
  var gFolder = sheet.getRange("outputFolderName").getValues();
  var gSpreadSheetName = sheet.getRange("outputSpreadsheetPrefix").getValues();
  var threadsPerSearch  = sheet.getRange("threadsPerSearch").getValues(); 

  // Number of emails per search:
  var threads = GmailApp.search("-label:" + gLabel, 0, threadsPerSearch); 
  
  for (var x=0; x<threads.length; x++) {
        
    var messages = threads[x].getMessages();
    
    // getting null...a thread without messages is weird
    if(messages != null) {
     for (var y=0; y<messages.length; y++) {
        
        updateSpreadSheet(messages[y]);
       
      }
    }
   
    GmailApp.getUserLabelByName(gLabel).addToThread(threads[x]);
  }
}

I am stuck in the middle of nowhere, have no clue about why this is happening?

Could anyone please guide to resolve the issue?

Rubén
  • 34,714
  • 9
  • 70
  • 166
MA1
  • 2,767
  • 5
  • 35
  • 51
  • 6
    Just try to simplify your script by commenting suspicious parts until it works and rebuild it step by step. Use the debugger and the execution transcript to see exactly where it fails... no doubt that when you'll find the issue it will appear so obvious.:-) – Serge insas Sep 14 '13 at 20:26
  • @Sergeinsas: I commented different parts of the function but issue is still there, I even commented everything in the function but the above error is still coming when i run that function. – MA1 Sep 15 '13 at 13:45
  • Should i post the function code? – MA1 Sep 15 '13 at 17:54
  • 1
    I am still researching, but I think this is a generic message for an unhandled back end error. – toddmo Apr 09 '20 at 23:09

6 Answers6

16

Problem Solved.

The problem is with string replace function in some other function. I am using the string replace function with regular expression as input to replace function. The regular expression was incorrect due which i am getting the above error.

That's really weird, at least GAS should give some proper error.

MA1
  • 2,767
  • 5
  • 35
  • 51
  • 7
    3.5 years on, the error still persists. I got the same error because I fed `undefined` variable to `Utilities.formatString`. GAS should really fix the debugging messages there. –  Jun 14 '17 at 05:54
  • Was getting this same error due to an invalid backreference. Good to know at least that GAS wasn't throttling usage. – David Metcalfe Oct 14 '17 at 21:36
  • 1
    Thanks for this. My case was `if( typeof i == 'string'){..}` where variable i turned out to be undefined. Changing it to `if( i && typeof i == 'string'){..}` did the trick. – coderofsalvation Apr 04 '19 at 10:29
  • 2
    I was facing the issue and was not getting any clue to solve it. The solution gave me a clue about how to proceed. In my case, some of the attributes of the object that i was passing to the server was having "NaN" as the value. Which was why it was not even entering the function. GAS should atleast show some meaningful errors. – Sukanya Pai Dec 30 '19 at 06:01
  • Almost 9 years now and the error still persists. I forgot to pass folder id to `DriveApp.getFolderById()` func – S.aad Jul 15 '22 at 15:41
15
  • In the script -> Resources -> Advanced google services -> turn on Drive API
  • Click in below message " Google Cloud Platform API Dashboard" to open thee cloud project for the script.
  • With the project selected, search in the search bar "Drive API" -> ENABLE, do the same for "Google Drive API".

Done, no more "we're are sorry server error" for DriveApp functions in the script.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Hemaka Raveen
  • 227
  • 5
  • 8
  • I don't see "Google Cloud Platform API Dashboard". Where is this? How else can we get here? – Mike Kormendy Dec 01 '20 at 18:28
  • Go to https://console.cloud.google.com. Then, go to navigation menu (in top of the left) and click the APIs & Service. Otherwise you can search APIs from the search bar. – Hemaka Raveen Dec 09 '20 at 05:04
  • This solution only might work when the script is using Google Drive. (See [Lexcel Atmadata's answer](https://stackoverflow.com/a/68409536/1595451) – Rubén Mar 06 '22 at 18:27
3

The answer provided by Hemaka Raveen seems correct, it helped me in the same problem (it appeared when one of my functions wanted to access API which was not enabled for me).

However in 2020 Google introduced new code editor for Apps Script, Hemaka Raveen's answer is related to the legacy editor. To enable Google Drive API in the new editor, open your script, find Services tab on the left side, click on the + icon, select Drive API and click Add.

Keep in mind that enabling it here is only one part of the proccess. You have to enable it in Google Cloud Platform, but that step is the same as in Hemaka Raveen's description.

Grzegorz Maj
  • 166
  • 1
  • 4
  • [Hemake Ranveen's answer](https://stackoverflow.com/a/64352044/1595451) only might work when the script is using Google Drive. (See [Lexcel Atmadata's answer](https://stackoverflow.com/a/68409536/1595451) – Rubén Mar 06 '22 at 18:27
1

It seems a general "I haven't got a clue what is going wrong" error message. In my case I added a library and other users of the script that used the library got this message, whatever function was called or trigger fired (even onOpen())

It was solved when I deployed the library as "version 1".

0

I googled this exact error, & found this page.

toddmo commented below question:

I am still researching, but I think this is a generic message for an unhandled back end error.

This made me check my whole script. I was calling a function at the end of script; which was calling itself in the function, creating a recursive call of that function. I fixed that bug, & no more this error.

DavChana
  • 1,944
  • 17
  • 34
0

Problem Solved.

For me, this turned out to be a legacy script trying to run an Advanced Google service. I made a copy of the Google sheet and then ran it to enable permissions.

If your script project uses a default GCP project created on or after April 8, 2019, the API is enabled automatically after you enable the advanced service and save the script project. If you have not done so already, you may also be asked to agree to the Google Cloud Platform and Google APIs Terms of Service as well.

Advanced Google services

James
  • 41
  • 4