UrlFetchApp を
Http Response 200 以外の
例外が
Google App Script の Version
Version 情報なんてあるのかとは
Relese Notes は
Release Notes | Apps Script | Google Developers
[1] Deprecated される
参考
google apps script - How to catch UrlFetchApp.fetch exception - Stack Overflow
UrlFetchApp のexception の ハンドリング方法に ついて Handle Errors in Apps Script with Try and Catch | Drive Bunny
exception 自体の扱いに ついて Google Apps Scriptの
UrlFetchAppで SSL証明書の エラーを 回避する | ハックノート
自己証明書エラーの回避方法 Google Apps Script: エラー情報の
取得 | kenlog
エラーオブジェクトのプロパティの 取得
muteHttpExceptions
に true
を 設定する
デフォルトだとmuteHttpExceptions
の
エラーを
StackOverFlow の
var payload = {"value": "key"}; var response = UrlFetchApp.fetch( url, { method: "PUT", contentType: "application/json", payload: JSON.stringify(payload), muteHttpExceptions: true, } ); var responseCode = response.getResponseCode(); var responseBody = response.getContentText(); if (responseCode === 200) { var responseJson = JSON.parse(responseBody); // ... } else { Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody)); // ... }
try~catch
する
現在作成している
var errors = new Array(); for each (var sitemapUrl in sitemapUrls) { for each (var target in pingTargets) { try { var requestUrl = target + "?sitemap="; var requestUrl = requestUrl + encodeURIComponent(sitemapUrl); info("URL:>>>" + sitemapUrl + "|Target:>>>" + target); // GETリクエスト var response = UrlFetchApp.fetch(requestUrl); info("ResponseCode:>>" + response.getResponseCode()); } catch (ex) { errors.push("Message: " + ex.message + "\r\nFile: " + ex.fileName + "\r\nLine: " + ex.lineNumber + "\r\n"); } } } if(errors.length > 0) { var recipients = getColumValues("config", "C", 1); sendEmails(recipients, "Ping sitemap error report", errors.join("\r\n")); }
例外をex.fileName
とか、ex.lineNumber
等で、
ex.message
を
muteHttpExceptions
に true
を 設定しても Exceptionは 発生する
muteHttpExceptions
にtrue
を
google apps script - UrlFetchApp muteHttpException doesnt work - Stack Overflow
にも
実際に
テストスクリプトを
テストスクリプトでは、
Google Apps Scriptの
を"validateHttpsCertificates" : false
を
DNSエラー
- 実装
function testUrlFetchAppDNSError() { var url = "https://localhost.unknown"; var options = { "muteHttpExceptions" : true, "validateHttpsCertificates" : false, "followRedirects" : false, } try { var response = UrlFetchApp.fetch(url, options); Logger.info(url + ":responscode>>>" + response.getResponseCode()); } catch(e) { Logger.log("message:" + e.message + "\nfileName:" + e.fileName + "\nlineNumber:" + e.lineNumber + "\nstack:" + e.stack); } }
- 実装
[17-09-01 22:08:07:237 JST] message:DNS エラー: https://localhost.unknown fileName:コード lineNumber:22 stack: at コード:22 (testUrlFetchAppDNSError)
不正なURL
実装
function testInvalidURL() { var url = "https://^/localhost.com"; var options = { "muteHttpExceptions" : true, "validateHttpsCertificates" : false, "followRedirects" : false, } try { var response = UrlFetchApp.fetch(url, options); Logger.info(url + ":responscode>>>" + response.getResponseCode()); } catch(e) { Logger.log("message:" + e.message + "\nfileName:" + e.fileName + "\nlineNumber:" + e.lineNumber + "\nstack:" + e.stack); } }
エラーログ
[17-09-01 22:31:39:835 JST] message:無効な引数: https://^/localhost.com fileName:コード lineNumber:22 stack: at コード:22 (testInvalidURL)
使用できない アドレス
これは、
アクセスしている
推測では
実装
function testUrlFetchAppUnsedAddress() { var url = "https://localhost.com"; var options = { "muteHttpExceptions" : true, "validateHttpsCertificates" : false, "followRedirects" : false, } try { var response = UrlFetchApp.fetch(url, options); Logger.info(url + ":responscode>>>" + response.getResponseCode()); } catch(e) { Logger.log("message:" + e.message + "\nfileName:" + e.fileName + "\nlineNumber:" + e.lineNumber + "\nstack:" + e.stack); } }
エラーログ
[17-09-01 22:06:19:125 JST] message:使用できないアドレス: https://localhost.com fileName:コード lineNumber:22 stack: at コード:22 (testUrlFetchAppDNSError)
Exception が"muteHttpExceptions" : true
だけでなく、try~catch
も
以上です。
コメント