changed error handling to try providing most useful messages

This commit is contained in:
tigerbot 2017-09-28 13:50:52 -06:00
parent db284fbf91
commit 704337e30b
2 changed files with 17 additions and 11 deletions

View File

@ -892,15 +892,14 @@
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
} }
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
var data;
if (xhr.readyState !== XMLHttpRequest.DONE) { if (xhr.readyState !== XMLHttpRequest.DONE) {
// nothing to do here // nothing to do here
return; return;
} }
var data, err;
if (xhr.status !== 200) { if (xhr.status !== 200) {
reject(new Error('bad status code: ' + xhr.status)); err = new Error('bad status code: ' + xhr.status);
return;
} }
try { try {
@ -909,6 +908,15 @@
data = xhr.responseText; data = xhr.responseText;
} }
if (data.error) {
err = new Error(data.error.message || data.error_description || JSON.stringify(data.error));
}
if (err) {
err.result = data;
reject(err);
return;
}
resolve({ resolve({
_request: xhr _request: xhr
, headers: null // TODO , headers: null // TODO

View File

@ -59,10 +59,7 @@ OAUTH3._node._parseJson = function (resp) {
// TODO toCamelCase // TODO toCamelCase
if (!(resp.statusCode >= 200 && resp.statusCode < 400)) { if (!(resp.statusCode >= 200 && resp.statusCode < 400)) {
// console.log('[A3] DEBUG', resp.body);
err = new Error("bad response code: " + resp.statusCode); err = new Error("bad response code: " + resp.statusCode);
err.result = resp.body;
return PromiseA.reject(err);
} }
//console.log('resp.body', typeof resp.body); //console.log('resp.body', typeof resp.body);
@ -70,15 +67,16 @@ OAUTH3._node._parseJson = function (resp) {
try { try {
json = JSON.parse(json); json = JSON.parse(json);
} catch(e) { } catch(e) {
err = new Error('response not parsable:' + resp.body); err = err || (new Error('response not parsable: ' + resp.body));
err.result = resp.body;
return PromiseA.reject(err);
} }
} }
// handle both Oauth2- and node-style errors // handle both Oauth2- and node-style errors
if (json.error) { if (json && json.error) {
err = new Error(json.error && json.error.message || json.error_description || json.error); err = new Error(json.error.message || json.error_description || JSON.stringify(json.error));
}
if (err) {
err.result = json; err.result = json;
return PromiseA.reject(err); return PromiseA.reject(err);
} }