From 704337e30b6781412c6e97c78d6aeaccdbef3371 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Thu, 28 Sep 2017 13:50:52 -0600 Subject: [PATCH] changed error handling to try providing most useful messages --- oauth3.core.js | 14 +++++++++++--- oauth3.node.js | 14 ++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/oauth3.core.js b/oauth3.core.js index a31940e..2a77c9f 100644 --- a/oauth3.core.js +++ b/oauth3.core.js @@ -892,15 +892,14 @@ xhr = new XMLHttpRequest(); } xhr.onreadystatechange = function () { - var data; if (xhr.readyState !== XMLHttpRequest.DONE) { // nothing to do here return; } + var data, err; if (xhr.status !== 200) { - reject(new Error('bad status code: ' + xhr.status)); - return; + err = new Error('bad status code: ' + xhr.status); } try { @@ -909,6 +908,15 @@ 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({ _request: xhr , headers: null // TODO diff --git a/oauth3.node.js b/oauth3.node.js index 95b0bd3..cd8d241 100644 --- a/oauth3.node.js +++ b/oauth3.node.js @@ -59,10 +59,7 @@ OAUTH3._node._parseJson = function (resp) { // TODO toCamelCase if (!(resp.statusCode >= 200 && resp.statusCode < 400)) { - // console.log('[A3] DEBUG', resp.body); err = new Error("bad response code: " + resp.statusCode); - err.result = resp.body; - return PromiseA.reject(err); } //console.log('resp.body', typeof resp.body); @@ -70,15 +67,16 @@ OAUTH3._node._parseJson = function (resp) { try { json = JSON.parse(json); } catch(e) { - err = new Error('response not parsable:' + resp.body); - err.result = resp.body; - return PromiseA.reject(err); + err = err || (new Error('response not parsable: ' + resp.body)); } } // handle both Oauth2- and node-style errors - if (json.error) { - err = new Error(json.error && json.error.message || json.error_description || json.error); + if (json && json.error) { + err = new Error(json.error.message || json.error_description || JSON.stringify(json.error)); + } + + if (err) { err.result = json; return PromiseA.reject(err); }