mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-30 09:47:50 +01:00
fix error handling for Fetch API
This commit is contained in:
@@ -6,5 +6,5 @@ import { toast } from '../modules/front_end_components/Toast.js';
|
||||
gameHandler(io('/in-game'), window, gameTemplate).then(() => {
|
||||
toast('Connecting...', 'warning', true, false);
|
||||
}).catch((e) => {
|
||||
toast(e, 'error', true);
|
||||
toast(e.message, 'error', true, false);
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -28,13 +28,31 @@ const joinHandler = (e) => {
|
||||
if (validateName(name)) {
|
||||
sendJoinRequest(e, name, accessCode)
|
||||
.then((res) => {
|
||||
res.json().then(json => {
|
||||
UserUtility.setAnonymousUserId(json.cookie, json.environment);
|
||||
resetJoinButtonState(e, res, joinHandler);
|
||||
window.location = '/game/' + accessCode;
|
||||
});
|
||||
}).catch((res) => {
|
||||
handleJoinError(e, res, joinHandler);
|
||||
if (!res.ok) {
|
||||
switch (res.status) {
|
||||
case 404:
|
||||
toast('Game not found', 'error', true);
|
||||
break;
|
||||
case 400:
|
||||
res.text().then(text => {
|
||||
toast(text, 'error', true);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
toast('There was a problem joining the game', 'error', true);
|
||||
break;
|
||||
}
|
||||
resetJoinButtonState(e, joinHandler);
|
||||
} else {
|
||||
res.json().then(json => {
|
||||
UserUtility.setAnonymousUserId(json.cookie, json.environment);
|
||||
resetJoinButtonState(e, joinHandler);
|
||||
window.location = '/game/' + accessCode;
|
||||
});
|
||||
}
|
||||
}).catch(() => {
|
||||
toast('Problems with the server are preventing your request to join the game.', 'error', true);
|
||||
resetJoinButtonState(e, joinHandler);
|
||||
});
|
||||
} else {
|
||||
toast('Name must be between 1 and 30 characters.', 'error', true, true, 'long');
|
||||
@@ -65,28 +83,12 @@ function sendJoinRequest (e, name, accessCode) {
|
||||
);
|
||||
}
|
||||
|
||||
function resetJoinButtonState (e, res, joinHandler) {
|
||||
function resetJoinButtonState (e, joinHandler) {
|
||||
document.getElementById('join-game-form').onsubmit = joinHandler;
|
||||
e.submitter.classList.remove('submitted');
|
||||
e.submitter.setAttribute('value', 'Join');
|
||||
}
|
||||
|
||||
function handleJoinError (e, res, joinHandler) {
|
||||
resetJoinButtonState(e, res, joinHandler);
|
||||
|
||||
if (res.status === 404) {
|
||||
toast('This game was not found.', 'error', true, true, 'long');
|
||||
} else if (res.status === 400) {
|
||||
toast(res.content, 'error', true, true, 'long');
|
||||
} else if (res.status >= 500) {
|
||||
toast(
|
||||
'The server is experiencing problems. Please try again later',
|
||||
'error',
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function validateName (name) {
|
||||
return typeof name === 'string' && name.length > 0 && name.length <= 30;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user