Skip to content

Commit

Permalink
CLI support for handling invalid responses (#95)
Browse files Browse the repository at this point in the history
Previously the CLI would exit immediately with an error if a snake
returned a response with invalid JSON.

Now the CLI continues running the game and a snake with an invalid
response continues in the direction of it's last move.

Also logs a warning with details about the invalid response. Where
possible, the log includes the response body in case it contains helpful
info.
  • Loading branch information
jlafayette authored Jul 27, 2022
1 parent e1289af commit f953f87
Showing 1 changed file with 51 additions and 19 deletions.
70 changes: 51 additions & 19 deletions cli/commands/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,26 +398,58 @@ func (gameState *GameState) getMoveForSnake(boardState *rules.BoardState, snakeS
log.Printf("POST %s: %v", u, string(requestBody))
}
res, err := gameState.httpClient.Post(u.String(), "application/json", bytes.NewBuffer(requestBody))
move := snakeState.LastMove

// Use snake's last move as the default in case of an error
snakeMove := rules.SnakeMove{ID: snakeState.ID, Move: snakeState.LastMove}

if err != nil {
log.Printf("[WARN]: Request to %v failed\n", u.String())
log.Printf("Body --> %v\n", string(requestBody))
} else if res.Body != nil {
defer res.Body.Close()
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
} else {
playerResponse := client.MoveResponse{}
jsonErr := json.Unmarshal(body, &playerResponse)
if jsonErr != nil {
log.Fatal(jsonErr)
} else {
move = playerResponse.Move
}
}
}
return rules.SnakeMove{ID: snakeState.ID, Move: move}
log.Printf(
"[WARN]: Request to %v failed\n"+
"\tError: %s\n", u.String(), err)
return snakeMove
}
if res.Body == nil {
log.Printf(
"[WARN]: Failed to parse response from %v\n"+
"\tError: body is empty\n", u.String())
return snakeMove
}
defer res.Body.Close()
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Printf(
"[WARN]: Failed to read response body from %v\n"+
"\tError: %v\n", u.String(), readErr)
return snakeMove
}
if res.StatusCode != http.StatusOK {
log.Printf(
"[WARN]: Got non-ok status code from %v\n"+
"\tStatusCode: %d (expected %d)\n"+
"\tBody: %q\n", u.String(), res.StatusCode, http.StatusOK, body)
return snakeMove
}
playerResponse := client.MoveResponse{}
jsonErr := json.Unmarshal(body, &playerResponse)
if jsonErr != nil {
log.Printf(
"[WARN]: Failed to decode JSON from %v\n"+
"\tError: %v\n"+
"\tBody: %q\n"+
"\tSee https://docs.battlesnake.com/references/api#post-move", u.String(), jsonErr, body)
return snakeMove
}
if playerResponse.Move != "up" && playerResponse.Move != "down" && playerResponse.Move != "left" && playerResponse.Move != "right" {
log.Printf(
"[WARN]: Failed to parse JSON data from %v\n"+
"\tError: invalid move %q, valid moves are \"up\", \"down\", \"left\" or \"right\"\n"+
"\tBody: %q\n"+
"\tSee https://docs.battlesnake.com/references/api#post-move", u.String(), playerResponse.Move, body)
return snakeMove
}

snakeMove.Move = playerResponse.Move
return snakeMove
}

func (gameState *GameState) sendEndRequest(boardState *rules.BoardState, snakeState SnakeState) {
Expand Down

0 comments on commit f953f87

Please sign in to comment.