7

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");
  }
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Meta Morfoself
  • 103
  • 1
  • 2
  • 9

2 Answers2

4

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.

fooby
  • 851
  • 5
  • 6
  • hi fooby thx for your response, i handle the error when page wass reset connection using try{}catch(){} ... and handle another error using getResponseCode() thats good solution?? sorry if iam missunderstood... – Meta Morfoself Dec 26 '12 at 16:03
  • handling network type errors like timeouts and page not found etc must be handled in a try catch. response errors like 404 not found, or 503 service unavailable etc are caught unless you have the `muteHttpExceptions` set true. in that case, to handle the error, you must read the content of the message and make up your own mind. hopefully that clarifies things – fooby Dec 26 '12 at 16:25
1

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

Steven Pribilinskiy
  • 1,862
  • 1
  • 19
  • 21