Skip to content

Commit

Permalink
Handle empty responses from XHR in a more robust way.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 679771706
  • Loading branch information
sufyanAbbasi authored and Google Earth Engine Authors committed Sep 27, 2024
1 parent 3a0027f commit f2e8070
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions javascript/src/apiclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,9 @@ apiclient.handleResponse_ = function(
return {parsed: response};
} catch (e) {
if (body === '') {
// XHR returns an empty string for large responses (>100 MB).
return 'Response was too large to parse. ' +
'Hint: Use limit() to fetch less elements of a collection.';
// XHR returns an empty string for large responses (>100 MB) or when
// there is a network error. Handle this case in the caller.
return '';
}
return 'Invalid JSON: ' + body;
}
Expand All @@ -1073,6 +1073,8 @@ apiclient.handleResponse_ = function(
} else if (status < 200 || status >= 300) {
return 'Server returned HTTP code: ' + status + ' for ' + method + ' ' +
url;
} else {
return '';
}
};

Expand All @@ -1084,7 +1086,11 @@ apiclient.handleResponse_ = function(
if (response.parsed) {
data = response.parsed;
} else {
errorMessage = response;
// Empty response bodies are handled here: return the status error if
// there is one, otherwise the long response error.
const responseTooLargeMessage = 'Response was too large to parse. ' +
'Hint: Use limit() to fetch less elements of a collection.';
errorMessage = response || statusError(status) || responseTooLargeMessage;
}
} else if (contentType === 'multipart/mixed') {
data = {};
Expand Down

0 comments on commit f2e8070

Please sign in to comment.