Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 519609a

Browse files
sandereversblakeembrey
authored andcommitted
Parse body on error object (#34)
1 parent b6db011 commit 519609a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/client-oauth2.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ function handleAuthResponse (data) {
133133
return Promise.resolve(data)
134134
}
135135

136+
/**
137+
* Attempt to parse response body as JSON, fall back to parsing as a query string.
138+
*
139+
* @param {String} body
140+
* @return {Object}
141+
*/
142+
function parseResponseBody (body) {
143+
try {
144+
return JSON.parse(body)
145+
} catch (e) {
146+
return Querystring.parse(body)
147+
}
148+
}
149+
136150
/**
137151
* Sanitize the scopes option to be a string.
138152
*
@@ -266,16 +280,10 @@ ClientOAuth2.prototype._request = function (options) {
266280
if (res.status < 200 || res.status >= 399) {
267281
var err = new Error('HTTP status ' + res.status)
268282
err.status = res.status
269-
err.body = res.body
283+
err.body = parseResponseBody(res.body)
270284
return Promise.reject(err)
271285
}
272-
273-
// Attempt to parse as JSON, fall back to parsing as a query string.
274-
try {
275-
return JSON.parse(res.body)
276-
} catch (e) {
277-
return Querystring.parse(res.body)
278-
}
286+
return parseResponseBody(res.body)
279287
})
280288
}
281289

0 commit comments

Comments
 (0)