Check HTTP status before parsing body in fetchJson (#280).

This commit is contained in:
Richard Moore 2018-09-20 12:44:46 -04:00
parent b9829f205f
commit 892f30a051
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295

@ -113,6 +113,15 @@ export function fetchJson(connection: string | ConnectionInfo, json: string, pro
request.onreadystatechange = function() {
if (request.readyState !== 4) { return; }
if (request.status != 200) {
cancelTimeout();
// @TODO: not any!
let error: any = new Error('invalid response - ' + request.status);
error.statusCode = request.status;
reject(error);
return;
}
let result: any = null;
try {
result = JSON.parse(request.responseText);
@ -140,15 +149,6 @@ export function fetchJson(connection: string | ConnectionInfo, json: string, pro
}
}
if (request.status != 200) {
cancelTimeout();
// @TODO: not any!
let error: any = new Error('invalid response - ' + request.status);
error.statusCode = request.status;
reject(error);
return;
}
cancelTimeout();
resolve(result);
};