admin管理员组

文章数量:1208155

I'm still new to JavaScript and Google apps script, and this is the first time I'm trying to use a 'Try/Catch' statement.

I'm running a script that connects to a page. It connects most of the time without issue, but occasionally it won't respond and throw a http error (Or, the response will be empty). I want to try/catch this response to have it run a second time if I get an error code, but I'm not 100% sure I understand the syntax, because no matter how I format it, It either never throws the exception, or always throws it.

Here is some sample code I've been experimenting with:

function myFunction() {
  var response =  UrlFetchApp.fetch("google"); 
  Logger.log('Response Code: ' + response.getResponseCode());

try {if(response.getResponseCode() === 200);
} catch (err) {
    throw 'Page connected';
}
}

If I can get this to work, I'm sure I can figure out the rest. However, even though the log shows me I get a HTTP response of 200, it never throws the error 'Page connected'.

If someone can guide me on:

1) Is this the correct method to achieve what I want, or is Try/Catch for something else? 2) The correct syntax.

I would be very grateful.

I'm still new to JavaScript and Google apps script, and this is the first time I'm trying to use a 'Try/Catch' statement.

I'm running a script that connects to a page. It connects most of the time without issue, but occasionally it won't respond and throw a http error (Or, the response will be empty). I want to try/catch this response to have it run a second time if I get an error code, but I'm not 100% sure I understand the syntax, because no matter how I format it, It either never throws the exception, or always throws it.

Here is some sample code I've been experimenting with:

function myFunction() {
  var response =  UrlFetchApp.fetch("google.com"); 
  Logger.log('Response Code: ' + response.getResponseCode());

try {if(response.getResponseCode() === 200);
} catch (err) {
    throw 'Page connected';
}
}

If I can get this to work, I'm sure I can figure out the rest. However, even though the log shows me I get a HTTP response of 200, it never throws the error 'Page connected'.

If someone can guide me on:

1) Is this the correct method to achieve what I want, or is Try/Catch for something else? 2) The correct syntax.

I would be very grateful.

Share Improve this question asked Oct 10, 2014 at 12:48 HDCerberusHDCerberus 2,1434 gold badges22 silver badges36 bronze badges 2
  • 1 And for something like this automatic retries can be helpful (exponential backup - gist.github.com/peterherrmann/2700284) – Andrew Roberts Commented Oct 13, 2014 at 7:10
  • @AndrewRoberts, Thanks, the script use the built in triggers to re-run every 5 minutes, so I think this won't be necessary. – HDCerberus Commented Oct 13, 2014 at 12:13
Add a comment  | 

4 Answers 4

Reset to default 9

getResponseCode does not throw an exception but fetch does throw an exception, so include it inside your try block:

function myFunction() {
  try {
    var response =  UrlFetchApp.fetch("google.com"); 
    Logger.log('Response Code: ' + response.getResponseCode());

    if(response.getResponseCode() === 200) {
      // something
    }
  } catch (err) {
      // handle the error here
  }
}

As of today, one can put muteHttpExceptions parameter in options of UrlFetchApp.fetch(url, options). It mutes HTTP exceptions.

With that, there is no need for try catch - just check whether response code is alright.

You can use option muteHttpExceptions to handle the exception yourself.

According to docs:

muteHttpExceptions: If true the fetch doesn't throw an exception if the response code indicates failure, and instead returns the HTTPResponse. The default is false.

Of course, here is the example code:

function DoSomething(url)
{
  var options = 
  {
  'muteHttpExceptions' : true
  };
  var response = UrlFetchApp.fetch(url, options);
  if(response.getResponseCode() == 200)
  {
    //Do anything with url data...
    return "Success!!!";
  }
  else
  {
    console.log("Response Code: " + response.getResponseCode());
    console.log("Content Text:\n" + response.getContentText());
    return "Failed :(";
  }
    
}

Here is what worked out fine for me, returning the HTTP code regardless of the page correct loading:

function getStatusCode(url){
    var options = {
        'followRedirects' : false
    };
    try{
        var response = UrlFetchApp.fetch(url,options);

        return response.getResponseCode();
    } catch (err) {
        // sample error content: "returned code 404."
        var regExp = new RegExp("returned code ([0-9]{3})","gmi"); 
        return regExp.exec(err)[1];
    }
}

本文标签: javascriptBest way to TryCatch HTTP response in Google ScriptStack Overflow