Skip to content

Commit

Permalink
Don't try to parse the response if it's empty
Browse files Browse the repository at this point in the history
  • Loading branch information
asynts committed Oct 7, 2022
1 parent 22cb49c commit 543764c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down

0 comments on commit 543764c

Please sign in to comment.