Google Apps Script: “The JavaScript runtime exited unexpectedly” when using UrlFetchApp

I’m working on a Google Apps Script that interacts with the Canvas Data 2 API and Google Cloud Storage. The script retrieves data from the API, processes it, and uploads it to Google Cloud Storage, eventually loading it into BigQuery.

The problem arises when I set the trigger to run the script hourly. Most of the time, the script fails with the error “The JavaScript runtime exited unexpectedly.” This error consistently occurs after the script logs the blob size, the ID of the stored file in Google Drive, and the fetched URL of the Google Cloud Storage. Please refer to the uploaded image.(https://i.sstatic.net/M3uiZvpB.jpg)

Here is the relevant part of my script:

// Function to request the URL to download the zip file. 

function requestObjectUrl(object_id) {
  console.log("Starting requestObjectUrl with object_id: " + object_id);

  const accessToken = getAccessToken(); 
  const apiUrl = 'https://api-gateway.instructure.com/dap/object/url';
  const payload = JSON.stringify([{"id": object_id}]);

  const options = {
    method: 'post',
    headers: {
      Authorization: `Bearer ${accessToken}`,
      'Content-Type': 'application/json'
    },
    muteHttpExceptions: true,
    payload: payload
  };

  try {
    console.log("Sending POST request to: " + apiUrl);
    console.log("Payload: " + payload);
    console.log("Options: " + JSON.stringify(options, null, 2));

    const response = UrlFetchApp.fetch(apiUrl, options);
    var data = JSON.parse(response.getContentText());

    if (data.urls && data.urls[object_id] && data.urls[object_id].url) {
      console.log("Received URL: " + data.urls[object_id].url);

      var fileResponse = UrlFetchApp.fetch(data.urls[object_id].url);
      var blob = fileResponse.getBlob();
      var blobSize = blob.getBytes().length;

      console.log("Blob size: " + blobSize + " bytes.");

      var fileName = "ファイル名.zip";
      var folderId = "1QDBwdHWxfmq7PDRPD7HIfjEVpP6rPra5";
      var folder = DriveApp.getFolderById(folderId);
      var file = folder.createFile(blob.setName(fileName));
      var fileId = file.getId();
      var finalFileName = fileId + ".gz";
      file.setName(finalFileName);

      console.log("File stored in Google Drive with ID: " + fileId);
      return fileId;
    } else {
      console.log("Error: URL not found in response. Full response: " + JSON.stringify(data, null, 2));
      return null;
    }
  } catch (e) {
    console.log("Error in requestObjectUrl function: " + e.toString());
    console.log("Stack trace: " + e.stack);
    return null;
  }
}

I have set detailed logging to understand where the script might be failing. The logs show the process reaches the point where the blob is fetched and its size is logged, the file is stored in Google Drive, and the fetched URL of the GCS is logged. However, the script then fails with “The JavaScript runtime exited unexpectedly.”

I suspect the issue might be in the requestObjectUrl function, but I am unable to pinpoint the exact cause. I expect the script to run hourly without encountering this runtime error. Here are some steps I have already tried:

  1. Adding extensive logging to trace the issue.
  2. Verifying the API calls and responses.
  3. Ensuring the payload and headers are correctly formatted.
  4. Checking for any network or timeout issues.
  5. Changing the trigger from hourly to every 12 hours, 6 hours, 4 hours, etc.
  6. Tested the script with a different data table instead of ‘web_logs’, however only ‘web_logs’ fail when I run it the script.

I have also provided an image a screenshot of the ‘Executions’ page. Function I ran, the type of excecution, the start date, duration of the execution and status (Failed/Completed
As you can see, I don’t suspect the duration of execution is a suspect in this issue, as it varies between the ‘completed’ and ‘failed’ executions. The blob size for the completed operations are usually less than 30 MB, and commonly below 15 MB. For the failed operations, the blob size is usually between 31 MB~70 MB. But commonly closer to 31 MB.

Please let me know if you need me to provide more details. Any insights or suggestions to resolve this issue would be greatly appreciated.

New contributor

Moataz Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật