Skip to content

Commit

Permalink
Send errors in backend using new error translation format
Browse files Browse the repository at this point in the history
- Remove pong interval for admin client
  • Loading branch information
joshzcold committed Dec 29, 2024
1 parent ee12f02 commit a07b1c3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion backend/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func (c *Client) readPump() {
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
err = EventPipe(c, message)
if err != nil {
errorMessage, err := NewSendError(fmt.Sprintf("Error reading socket message: %s", fmt.Sprint(err)))
log.Println(fmt.Sprint(err))
errorMessage, err := NewSendError(SERVER_ERROR)
if err != nil {
c.send <- []byte(fmt.Sprintf("%s", fmt.Sprint(err)))
return
Expand Down
21 changes: 21 additions & 0 deletions backend/api/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

type ErrorCode string

const (
ROOM_NOT_FOUND ErrorCode = "errors.room_not_found"
PARSE_ERROR ErrorCode = "errors.parse_error"
GAME_CLOSED ErrorCode = "errors.game_closed"
HOST_QUIT ErrorCode = "errors.host_quit"
IMAGE_TOO_LARGE ErrorCode = "errors.image_too_large"
CSV_TOO_LARGE ErrorCode = "errors.csv_too_large"
CSV_INVALID_FORMAT ErrorCode = "errors.csv_invalid_format"
UNKNOWN_FILE_TYPE ErrorCode = "errors.unknown_file_type"
SERVER_ERROR ErrorCode = "errors.server_error"
BUZZER_REGISTER_ERROR ErrorCode = "errors.buzzer_register"
SPECTATOR_REGISTER_ERROR ErrorCode = "errors.spectator_register"
CHANGE_LANG_ERROR ErrorCode = "errors.change_lang"
CONNECTION_LOST ErrorCode = "errors.connection_lost"
MISSING_INPUT ErrorCode = "errors.missing_input"
UNABLE_TO_CONNECT ErrorCode = "errors.unable_to_connect"
)
2 changes: 1 addition & 1 deletion backend/api/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (r *room) gameTimeout() error {
return fmt.Errorf(" %w", err)
}
r.Hub.broadcast <- message
message, err = NewSendError("game closed, no activity for 1 hour")
message, err = NewSendError(GAME_CLOSED)
if err != nil {
return fmt.Errorf(" %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions backend/api/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func NewSendData(newGameData *game) ([]byte, error) {

type sendError struct {
Action string `json:"action"`
Message string `json:"message"`
Code ErrorCode `json:"code"`
}

func NewSendError(message string) ([]byte, error) {
func NewSendError(code ErrorCode) ([]byte, error) {
return json.Marshal(sendError{
Action: "error",
Message: message,
Code: code,
})
}

Expand Down
2 changes: 1 addition & 1 deletion backend/api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func quitHost(room *room, event *Event) error {
}
room.Hub.broadcast <- message

message, err = NewSendError("host quit the game")
message, err = NewSendError(HOST_QUIT)
if err != nil {
return fmt.Errorf(" %w", err)
}
Expand Down
6 changes: 0 additions & 6 deletions components/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,6 @@ export default function Admin(props) {
}
}, 1000);

const pongInterval = setInterval(() => {
console.debug("sending pong in admin");
send({ action: "pong" });
}, 5000);

const handleMessage = (evt) => {
var received_msg = evt.data;
let json = JSON.parse(received_msg);
Expand All @@ -443,7 +438,6 @@ export default function Admin(props) {
ws.current.addEventListener("message", handleMessage);
send({ action: "change_lang", data: i18n.language });
return () => {
clearInterval(pongInterval);
clearInterval(retryInterval);
ws.current.removeEventListener("message", handleMessage);
};
Expand Down

0 comments on commit a07b1c3

Please sign in to comment.