Skip to content

Commit

Permalink
Check for JSON before parsing JSON in errors
Browse files Browse the repository at this point in the history
The createError function was incorrectly assuming that response
bodies are in JSON format and attempting to extract useful details
from that body.  But, for some error cases, the body is not JSON
resulting in a error being thrown during the process of adding
details to the error.

This fix adds safeguards around the places that need JSON to enable
the createError method to extract the useful details.
  • Loading branch information
JimPatterson committed Oct 20, 2021
1 parent 7689a66 commit 6c725cd
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/OAuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ OAuthClient.prototype.createError = function createError(e, authResponse) {
e.originalMessage = e.message;

e.error = '';
if ('error' in authResponse.getJson()) {
if (authResponse.isJson() && 'error' in authResponse.getJson()) {
e.error = authResponse.getJson().error;
} else if (authResponse.response.statusText) {
e.error = authResponse.response.statusText;
Expand All @@ -594,7 +594,7 @@ OAuthClient.prototype.createError = function createError(e, authResponse) {
}

e.error_description = '';
if ('error_description' in authResponse.getJson()) {
if (authResponse.isJson() && 'error_description' in authResponse.getJson()) {
e.error_description = authResponse.getJson().error_description;
} else if (authResponse.response.statusText) {
e.error_description = authResponse.response.statusText;
Expand Down

0 comments on commit 6c725cd

Please sign in to comment.