Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlFetchApp muteHttpException doesn't work

How to handle urlfetch error? I'am trying muteHttpExceptions: true, but it doesn't work and the script breaks.

function myFunction() {
  var result = UrlFetchApp.fetch("http://www.soccer-wallpapers.net/soccer_wallpaper/barcelona_fc_barcelona_360.jpg ", {
muteHttpExceptions: true });
  Logger.log("code: " + result.getResponseCode());
  Logger.log("text: " + result.getContentText());
}

But I'm trying another error url to work fine.

http://img8.uploadhouse.com/fileuploads/2058/20586362b34151eb10a4001e1c44305fe41bff6.jpg

Solved handle using the try and catch method.

  try
  {
    var page = UrlFetchApp.fetch("http://www.soccer-wallpapers.net/soccer_wallpaper/barcelona_fc_barcelona_360.jpg");
  }
  catch(err)
  {
    Browser.msgBox("error");
  }
}
like image 388
Meta Morfoself Avatar asked Dec 21 '12 03:12

Meta Morfoself


2 Answers

This seems to be working as designed. The muteHttpExeptions is for doing just that. It tells the script to carry on as normal if it received an HTTP error status. In your first example code, the error is not an HTTP error. There is no web server to return an HTTP code of any type. The URL does not exist. The service that reaches out to the internet cannot continue, and there is no HTTP exception to be had.

When I visit http://www.soccer-wallpapers.net/soccer_wallpaper/barcelona_fc_barcelona_360.jpg, I get a timeout message. There was nothing to respond to my request. It appears that soccer-wallpapers.net is no longer responding to requests. It does have a DNS entry, but does not respond to ping from my location.

If you must handle bad URLs, potentially down servers and other server errors, then you will need to use a try{}catch(){} block. How you handle those errors is up you. The muteHttpExceptions flag is meant more for handling HTTP errors as part of your application instead of throwing errors at the script level.

like image 195
fooby Avatar answered Oct 01 '22 18:10

fooby


That's still remains an issue in functions that are called in cells where the try-catch in this case is just ignored

like image 39
Steven Pribilinskiy Avatar answered Oct 01 '22 16:10

Steven Pribilinskiy