fix error handling for Fetch API

This commit is contained in:
AlecM33
2023-08-13 18:47:38 -04:00
parent 0b7dd9f4d7
commit da0d69062e
9 changed files with 81 additions and 55 deletions

View File

@@ -31,7 +31,19 @@ function attemptToJoinGame (event) {
mode: 'cors'
}
).then((res) => {
if (res.status === 200) {
if (!res.ok) {
switch (res.status) {
case 404:
toast('Game not found', 'error', true);
break;
default:
toast('There was a problem joining the game', 'error', true);
break;
}
form.addEventListener('submit', attemptToJoinGame);
form.classList.remove('submitted');
document.getElementById('join-button')?.setAttribute('value', 'Join');
} else {
res.json().then(json => {
window.location = window.location.protocol + '//' + window.location.host +
'/join/' + encodeURIComponent(json.accessCode) +
@@ -39,17 +51,11 @@ function attemptToJoinGame (event) {
'&timer=' + encodeURIComponent(getTimeString(json.timerParams));
});
}
}).catch((res) => {
}).catch(() => {
form.addEventListener('submit', attemptToJoinGame);
form.classList.remove('submitted');
document.getElementById('join-button')?.setAttribute('value', 'Join');
if (res.status === 404) {
toast('Game not found', 'error', true);
} else if (res.status === 400) {
toast(res.content, 'error', true);
} else {
toast('An unknown error occurred. Please try again later.', 'error', true);
}
toast('An unknown error occurred. Please try again later.', 'error', true);
});
} else {
toast('Invalid code. Codes are 4 numbers or letters.', 'error', true, true);