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
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
this step needs the editor
on desktop today; in the app (coming soon). save your spot and we'll bring you back here when you're ready.