mirror of
https://github.com/AlecM33/Werewolf.git
synced 2026-01-01 16:59:29 +01:00
fix error handling for Fetch API
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user