Fix json parsing exceptions obscuring server errors (#1605)

I got a 413 (Rate limit exceeded) error from the server while fetching prekeys.
The client tried to parse the response as json since we expect json from the
prekey endpoint, which threw an exception because the response was not json.
This change prevents us from treating the response as json unless it has the
Content-Type header set accordingly.

If for some reason, the client and server disagree on whether the response
should be or is json, we'll default to treating it as text.

// FREEBIE
This commit is contained in:
Lilia 2017-10-24 15:54:46 -07:00 committed by Scott Nonnenberg
parent 1a2b123f31
commit d1f7f5ee8c
2 changed files with 8 additions and 6 deletions

View file

@ -37501,12 +37501,13 @@ var TextSecureServer = (function() {
} }
window.nodeFetch(url, fetchOptions).then(function(response) { window.nodeFetch(url, fetchOptions).then(function(response) {
var resultPromise; var resultPromise;
if (options.responseType === 'json') { if (options.responseType === 'json'
&& response.headers.get('Content-Type') === 'application/json') {
resultPromise = response.json(); resultPromise = response.json();
} else if (!options.responseType || options.responseType === 'text') {
resultPromise = response.text();
} else if (options.responseType === 'arraybuffer') { } else if (options.responseType === 'arraybuffer') {
resultPromise = response.buffer(); resultPromise = response.buffer();
} else {
resultPromise = response.text();
} }
return resultPromise.then(function(result) { return resultPromise.then(function(result) {
if (options.responseType === 'arraybuffer') { if (options.responseType === 'arraybuffer') {

View file

@ -64,12 +64,13 @@ var TextSecureServer = (function() {
} }
window.nodeFetch(url, fetchOptions).then(function(response) { window.nodeFetch(url, fetchOptions).then(function(response) {
var resultPromise; var resultPromise;
if (options.responseType === 'json') { if (options.responseType === 'json'
&& response.headers.get('Content-Type') === 'application/json') {
resultPromise = response.json(); resultPromise = response.json();
} else if (!options.responseType || options.responseType === 'text') {
resultPromise = response.text();
} else if (options.responseType === 'arraybuffer') { } else if (options.responseType === 'arraybuffer') {
resultPromise = response.buffer(); resultPromise = response.buffer();
} else {
resultPromise = response.text();
} }
return resultPromise.then(function(result) { return resultPromise.then(function(result) {
if (options.responseType === 'arraybuffer') { if (options.responseType === 'arraybuffer') {