javascript - Best way to Try/Catch HTTP response in Google Script -


I'm still new to JavaScript and Google Apps Script, and this is the first time that I try the 'try / hold' statement

I am running a script that connects to a page. It connects most of the time without any problems, but sometimes it will not respond and an HTTP error (or, the response will be empty) If I get an error code, then try this reaction to run it a second time. I want to catch / hold, but I'm not 100% sure that I understand the syntax, because it does not matter how I format it, it's either an exception. Does it, or always throws it.

Here are some sample code I have used:

  function myFunction () {var response = UrlFetchApp.fetch ("google.com"); Logger.log ('response code:' + response.getResponseCode ()); Try {if (response.getResponseCode () === 200); } Hold (mistake) {throw 'page linked'; }}   

If I can get it to work, I am sure I can find out the rest though, although Log shows me that I have an HTTP response of 200 It never gets thrown by 'linked to page' error.

If someone can guide me on this:

1) Is this the correct method of getting me , Or some other attempt / catch? 2) Correct Syntax

I would be very grateful

getResponseCode does not put an exception, but fetch throws an exception, so include it in your try block:

  function myFunction () {try {var response = UrlFetchApp.fetch ("google. Com "); Logger.log ('response code:' + response.getResponseCode ()); If (response.getResponseCode () === 200) {// some}} hold (mistake) {// handle error here}}    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -