promptdojo_

Status codes and error handling — what AI's API calls do when the wire blinks — step 6 of 9

Claude wrote a fetcher that calls .json() straight away without checking the status. The fake response below is a 401 with a JSON error body. The function returns data["name"], which doesn't exist in the error body, so it crashes with a KeyError — pointing at the wrong line.

Add an if response.status_code != 200: return None guard before the .json() call so the function returns None cleanly when the request failed.

Expected output:

None
The break is on line 8 — but read the whole snippet first.

full-screen editor opens — close anytime to keep reading.