From 543764c6d2bf76fa6918fc80bc326bde79fe06e5 Mon Sep 17 00:00:00 2001 From: Paul Scharnofske Date: Fri, 7 Oct 2022 17:11:44 +0200 Subject: [PATCH] Don't try to parse the response if it's empty --- src/server.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/server.js b/src/server.js index e391e77..ce99bca 100644 --- a/src/server.js +++ b/src/server.js @@ -11,13 +11,19 @@ async function spotifyApiRequest(method, path, body = undefined) { }), }); - let json = await response.json(); - - if (json.error?.status === 401 && json.error?.message === "The access token expired") { - if (await refreshAccessTokenAsync()) { - return spotifyApiRequest(method, path, body); - } else { - console.log("error: unable to refresh access token") + let text = await response.text(); + + // We have to be careful here, the server doesn't always return a JSON object. + let json = null; + if (text.length >= 1) { + json = JSON.parse(text); + + if (json.error?.status === 401 && json.error?.message === "The access token expired") { + if (await refreshAccessTokenAsync()) { + return spotifyApiRequest(method, path, body); + } else { + console.log("error: unable to refresh access token") + } } }