have worker send error back to client (#1178)

## Description:
On error, send the message back to the client before closing the
websocket.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

<DISCORD USERNAME>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
evanpelle
2025-06-17 20:13:37 -07:00
committed by GitHub
parent c052ab04e0
commit 4480871f65
6 changed files with 73 additions and 12 deletions
+24 -5
View File
@@ -13,6 +13,7 @@ import {
Intent,
PlayerRecord,
ServerDesyncSchema,
ServerErrorMessage,
ServerPrestartMessageSchema,
ServerStartGameMessageSchema,
ServerTurnMessageSchema,
@@ -195,7 +196,16 @@ export class GameServer {
this.log.error("Failed to parse client message", error, {
clientID: client.clientID,
});
client.ws.close(1002, "ClientMessageSchema");
client.ws.send(
JSON.stringify({
type: "error",
error: error.toString(),
} satisfies ServerErrorMessage),
);
// Add a small delay before closing the connection to ensure the error message is received
setTimeout(() => {
client.ws.close(1002, "ClientMessageSchema");
}, 100);
return;
}
const clientMsg = parsed.data;
@@ -543,11 +553,20 @@ export class GameServer {
clientID: client.clientID,
persistentID: client.persistentID,
});
client.ws.close(1000, "Kicked from game");
this.activeClients = this.activeClients.filter(
(c) => c.clientID !== clientID,
client.ws.send(
JSON.stringify({
type: "error",
error: "Kicked from game (you may have been playing on another tab)",
} satisfies ServerErrorMessage),
);
this.kickedClients.add(clientID);
// Add a small delay before closing the connection to ensure the error message is received
setTimeout(() => {
client.ws.close(1000, "Kicked from game");
this.activeClients = this.activeClients.filter(
(c) => c.clientID !== clientID,
);
this.kickedClients.add(clientID);
}, 100);
} else {
this.log.warn(`cannot kick client, not found in game`, {
clientID,
+7
View File
@@ -13,6 +13,7 @@ import {
ClientJoinMessageSchema,
GameRecord,
GameRecordSchema,
ServerErrorMessage,
} from "../core/Schemas";
import { CreateGameInputSchema, GameInputSchema } from "../core/WorkerSchemas";
import { archive, readGameRecord } from "./Archive";
@@ -300,6 +301,12 @@ export function startWorker() {
if (!parsed.success) {
const error = z.prettifyError(parsed.error);
log.warn("Error parsing join message client", error);
ws.send(
JSON.stringify({
type: "error",
error: error.toString(),
} satisfies ServerErrorMessage),
);
ws.close(1002, "ClientJoinMessageSchema");
return;
}