CLI support for handling invalid responses (#95)
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.
This commit is contained in:
parent
e1289af5fb
commit
f953f879bf
1 changed files with 50 additions and 18 deletions
|
|
@ -398,26 +398,58 @@ func (gameState *GameState) getMoveForSnake(boardState *rules.BoardState, snakeS
|
||||||
log.Printf("POST %s: %v", u, string(requestBody))
|
log.Printf("POST %s: %v", u, string(requestBody))
|
||||||
}
|
}
|
||||||
res, err := gameState.httpClient.Post(u.String(), "application/json", bytes.NewBuffer(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 {
|
if err != nil {
|
||||||
log.Printf("[WARN]: Request to %v failed\n", u.String())
|
log.Printf(
|
||||||
log.Printf("Body --> %v\n", string(requestBody))
|
"[WARN]: Request to %v failed\n"+
|
||||||
} else if res.Body != nil {
|
"\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()
|
defer res.Body.Close()
|
||||||
body, readErr := ioutil.ReadAll(res.Body)
|
body, readErr := ioutil.ReadAll(res.Body)
|
||||||
if readErr != nil {
|
if readErr != nil {
|
||||||
log.Fatal(readErr)
|
log.Printf(
|
||||||
} else {
|
"[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{}
|
playerResponse := client.MoveResponse{}
|
||||||
jsonErr := json.Unmarshal(body, &playerResponse)
|
jsonErr := json.Unmarshal(body, &playerResponse)
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
log.Fatal(jsonErr)
|
log.Printf(
|
||||||
} else {
|
"[WARN]: Failed to decode JSON from %v\n"+
|
||||||
move = playerResponse.Move
|
"\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
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return rules.SnakeMove{ID: snakeState.ID, Move: move}
|
snakeMove.Move = playerResponse.Move
|
||||||
|
return snakeMove
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gameState *GameState) sendEndRequest(boardState *rules.BoardState, snakeState SnakeState) {
|
func (gameState *GameState) sendEndRequest(boardState *rules.BoardState, snakeState SnakeState) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue