Handle JSON errors in a separate function

This commit is contained in:
Raphaël Jakse 2020-04-05 10:22:07 +02:00
parent e3532d151d
commit d5fd4c22a8
1 changed files with 19 additions and 14 deletions

View File

@ -470,6 +470,11 @@
);
}
function jsonError(json, e) {
console.error("An error ocurred while parsing this JSON message:", json);
fatalError(e);
}
function setRackCell(index, letter) {
setTileParent(playerLetters[index], letter);
}
@ -680,6 +685,14 @@
}
}
function parseAndHandleReceivedData(r) {
try {
handleReceivedData(JSON.parse(r));
} catch (e) {
jsonError(r, e);
}
}
function send(data) {
if (!pollingReady && ((data.cmds && data.cmds.length) && data.cmds[0].cmd !== "joinGame")) {
sendCmds();
@ -713,7 +726,7 @@
es.onmessage = function (e) {
pollingReady = true;
handleReceivedData(JSON.parse(e.data));
parseAndHandleReceivedData(e.data);
};
es.onerror = retryPolling;
@ -758,14 +771,10 @@
currentIndex = end;
expectedLength = 0;
} catch (e) {
console.error(
xhr.responseText.substring(
currentIndex,
end
)
);
fatalError(e);
jsonError(xhr.responseText.substring(
currentIndex,
end
), e);
}
retriedImmediately = false;
@ -785,11 +794,7 @@
return;
}
try {
handleReceivedData(JSON.parse(xhr.responseText));
} catch (e) {
fatalError(e);
}
parseAndHandleReceivedData(xhr.responseText);
blockMove--;