I am creating an extension in Google Spreadsheets and I am trying to handle the 404 error that urlFetch () can provide. Now it just shows a popup at the top of the table:
http://i.stack.imgur.com/fvCPC.png
This is the code I have:
function doTestAPICall() {
var headers = {
'Authorization': 'Basic ' + Utilities.base64Encode(user + "/token:" + token)
};
var url = "https://" + subdomain + ".domain.com/api/v2/users/me.json";
var options = {
'contentType': 'application/json',
'method': 'get',
'headers': headers
};
var response = UrlFetchApp.fetch(url, options);
if(response.getResponseCode() == 200){
var json = response.getContent();
var data = JSON.parse(json);
if(data.user.email != user){
return false;
} else {
return true;
}
}else{
return false;
}
}
However, the function never receives response.getResponseCode (), since it no longer works in the UrlFetchApp.fetch function. Any ideas / suggestions on how I can handle this?
Thank!
--- EDIT ---
As per Amit's suggestion, adding muteHttpExceptions in the UrlFetchApp parameters solved the problem.
Now the code is as follows:
function doTestAPICall() {
var headers = {
'Authorization': 'Basic ' + Utilities.base64Encode(user + "/token:" + token)
};
var url = "https://" + subdomain + ".domain.com/api/v2/users/me.json";
var options = {
'contentType': 'application/json',
'method': 'get',
'headers': headers,
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options);
if(response.getResponseCode() == 200){
var json = response.getContentText();
var data = JSON.parse(json);
if(data.user.email != user){
return false;
} else {
return true;
}
}else{
return false;
}
}
source
share