Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Jan 13, 2025
1 parent bf9da9e commit 935fe2a
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions web/src/common/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export function logOff() {
document.getElementById("toggleAdmin").innerHTML = "Switch to admin mode";
}

function handleFetchResponse(resp)
{
const contentType = resp.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
return resp.json();
} else {
return Promise.reject(`Code ${resp.status}`);
}
}

export async function getApiToken(pwd, onSuccess, onFailure) {
if (pwd === null) return;

Expand All @@ -74,7 +84,7 @@ export async function getApiToken(pwd, onSuccess, onFailure) {
},
body: JSON.stringify(pwd)
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
adminToken = json.token;
Expand Down Expand Up @@ -106,7 +116,7 @@ export async function generatePassword(pwd) {
},
body: JSON.stringify(pwd)
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
prompt("Hashed password, please paste it in your credentials file", json.token);
Expand All @@ -128,7 +138,7 @@ export async function uploadSong(data, onSuccess, onFailure) {
},
body: data
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
alert(`Success`);
Expand Down Expand Up @@ -156,7 +166,7 @@ export async function archiveSong(key, onSuccess, onFailure) {
},
body: data
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
onSuccess();
Expand Down Expand Up @@ -184,7 +194,7 @@ export async function favoriteSong(key, toggle, onSuccess, onFailure) {
},
body: data
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
onSuccess();
Expand All @@ -211,7 +221,7 @@ export async function repairSong(key, onSuccess, onFailure) {
},
body: data
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
onSuccess();
Expand All @@ -236,7 +246,7 @@ export async function updateSong(data, onSuccess, onFailure) {
},
body: data
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
alert(`Your song was successfully updated`);
Expand All @@ -260,7 +270,7 @@ export async function validateIntegrity() {
'Authorization': `Bearer ${adminToken}`
}
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
alert(`Integrity succeeded`);
Expand All @@ -285,7 +295,7 @@ export async function createPlaylist(name, onSuccess, onFailure) {
},
body: data
})
.then(resp => resp.ok ? resp.json() : Promise.reject(`Code ${resp.status}`))
.then(handleFetchResponse)
.then(json => {
if (json.success) {
onSuccess();
Expand Down

0 comments on commit 935fe2a

Please sign in to comment.