Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix CLI to handle bad responses #95

Merged

Conversation

jlafayette
Copy link
Contributor

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 bad 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.

Resolves BattlesnakeOfficial/feedback#205

Examples of logged warnings

Timeout:

2022/07/26 10:22:19 [WARN]: Request to http://localhost:5003/move failed
        Error: Post "http://localhost:5003/move": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

Bad status code (and non-JSON response)

2022/07/26 10:24:12 [WARN]: Got non-ok status code from http://165.227.35.82:5000/move
        StatusCode: 502 (expected 200)
        Body: "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Jester route exception</title></head><body><h1>An error has occured in one of your routes.</h1><p><b>Detail: </b>fatal.nim(53)            sysFatal<br/>\nasyncfutures.nim(389)    read<br/>\nasyncfutures.nim(389)    read<br/>\nindex 49 not in 0 .. 48</p></body></html>"

Non-JSON response (status code 200):

2022/07/26 10:26:09 [WARN]: Failed to decode JSON from http://localhost:5000/move
        Error: invalid character 'u' looking for beginning of value
        Body: "up"
        See https://docs.battlesnake.com/references/api#post-move

Bad value for move:

2022/07/26 10:27:36 [WARN]: Failed to parse JSON data from http://localhost:5000/move
        Error: invalid move "forward", valid moves are "up", "down", "left" or "right"
        Body: "{\n  \"move\": \"forward\"\n}\n"
        See https://docs.battlesnake.com/references/api#post-move

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.
@bvanvugt bvanvugt merged commit f953f87 into BattlesnakeOfficial:main Jul 27, 2022
@jlafayette jlafayette deleted the cli-handle-invalid-responses branch July 27, 2022 03:49
coreyja added a commit to jlafayette/rules that referenced this pull request Jul 31, 2022
* main:
  DEV-1558-healing-pools-map (BattlesnakeOfficial#94)
  DEV-1556-sinkholes-map (BattlesnakeOfficial#96)
  CLI support for handling invalid responses (BattlesnakeOfficial#95)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants