From 6c725cd6a0f111bfe89722ca96fcbe930d15500e Mon Sep 17 00:00:00 2001 From: Jim Patterson Date: Wed, 20 Oct 2021 12:47:55 -0400 Subject: [PATCH] Check for JSON before parsing JSON in errors 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. --- src/OAuthClient.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/OAuthClient.js b/src/OAuthClient.js index 9662c002..ee515a2f 100644 --- a/src/OAuthClient.js +++ b/src/OAuthClient.js @@ -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; @@ -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;