我想在HTTP请求失败时编写回调。如何将其链接到 UrlFetchApp.fetch((? 请参考下面的 HTTP 请求。
// Make a GET request.
UrlFetchApp.fetch('http://www.google.com/');
请注意,fetch
函数是同步的。它不提供回调参数,也不返回 promise。
可以通过UrlFetchApp.fetch(url, params)
函数捕获异常。可以将muteHttpExceptions
参数传递到函数调用的参数位置。这样,您可以自己检查响应代码并做出适当的响应。文档:https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params
UrlFetchApp.fetch('http://www.google.com/', {muteHttpExceptions: true});
muteHttpExceptions(布尔值( 如果设置为 true,则提取不会 如果响应代码指示失败,则引发异常,并将 而是返回 HTTPResponse(默认值:false(
另一种方法是简单的 try/catch 语句。我想您可以记录错误或做出适当的响应。
try {
UrlFetchApp.fetch('http://www.google.com/');
}
catch(e) {
// e contains error response
}