Handling API errors

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Handling API errors

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which status code means an API request worked?
A 200
B 404
2. Which line checks whether the request succeeded?
A if response.status_code = 200:
B if response.status_code == 200:
3. What does a status code of 404 tell your program?
A The requested resource was not found
B The request worked and the data is ready
4. Which code stops the program from crashing if the internet connection drops?
A response = requests.get(url) print("Could not connect")
B try: response = requests.get(url) except requests.exceptions.RequestException: print("Could not connect")
5. Which message is better for a user when the API call fails?
A Sorry, we could not load the data. Please try again.
B requests.exceptions.ConnectionError: HTTPSConnectionPool

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1response.status_code == 200 AChecks whether the API call succeeded, because status code 200 means OK and the data came back.
2except requests.exceptions.RequestException: Belif response.status_code == 404:
3A 404 status code means the resource was not found, so the program should tell the user rather than read the data. Ctry: response = requests.get(url)
4Wrap requests.get() in a try block so that a connection failure can be handled by an except block. DThe call is not in try/except and the status is not checked, so a failed call crashes the program or gives bad data.
5if response.status_code = 200: ECatches a failed API call, such as no internet or a timeout, so the program can show a friendly message instead of crashing.
6response = requests.get(url) print(response.json()) FA single = assigns a value, so it is a syntax error. Use == to compare the status code with 200.

Definitions

response.status_code

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if response.___________ == 200:
2.
___: response = requests.get(url) except requests.exceptions.RequestException:
3.
response = requests.get(url) if ________.status_code == 404: print("Not found")
4.
try: r = requests.get(url, timeout=5) except requests.exceptions.________________:
5.
except requests.exceptions.RequestException: _____("Could not connect.")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import requests
B url = 'https://x.com/api'
C r = requests.get(url)
D if r.status_code = 200:
E print('Success')
Question 2
A try:
B r = requests.get(url)
C except requests.Timeout
D print('Server too slow')
Question 3
A r = requests.get(url)
B if r.status_code == '404':
C print('Not found')
D else:
E print(r.json())
Question 4
A import request
B try:
C r = requests.get(url)
D except requests.Timeout:
E print('Try again later')
Question 5
A r = requests.get(url)
B if r.status_code == 200:
C data = r.json
D else:
E print('Failed')

Answer Key

Handling API errors
Section A: Quick Questions
Q1
A — 200
Q2
B — if response.status_code == 200:
Q3
A — The requested resource was not found
Q4
B — try: response = requests.get(url) except requests.exceptions.RequestException: print("Could not connect")
Q5
A — Sorry, we could not load the data. Please try again.
Section B: Match the Pairs
Q1
response.status_code == 200 → Checks whether the API call succeeded, because status code 200 means OK and the data came back.
Q2
except requests.exceptions.RequestException: → Catches a failed API call, such as no internet or a timeout, so the program can show a friendly message instead of crashing.
Q3
A 404 status code means the resource was not found, so the program should tell the user rather than read the data. → elif response.status_code == 404:
Q4
Wrap requests.get() in a try block so that a connection failure can be handled by an except block. → try: response = requests.get(url)
Q5
if response.status_code = 200: → A single = assigns a value, so it is a syntax error. Use == to compare the status code with 200.
Q6
response = requests.get(url) print(response.json()) → The call is not in try/except and the status is not checked, so a failed call crashes the program or gives bad data.
Section C: Definitions
Q1
response.status_code — Check a response's status code (200 OK, 404 not found) and wrap requests.get() in try/except so a failed API call shows a friendly message, not a crash.
Section D: Fill in the Blanks
Q1
if response.status_code == 200:
Q2
try: response = requests.get(url) except requests.exceptions.RequestException:
Q3
response = requests.get(url) if response.status_code == 404: print("Not found")
Q4
try: r = requests.get(url, timeout=5) except requests.exceptions.RequestException:
Q5
except requests.exceptions.RequestException: print("Could not connect.")
Section E: Spot the Error
Q1
D — A single = assigns a value and is a syntax error in an if condition. Use == to compare the status code with 200.
Q2
C — The except line must end with a colon. It should be except requests.Timeout:
Q3
B — status_code is an integer, so comparing it with the string '404' is never true. It should be == 404 with no quotes.
Q4
A — The library is called requests, with an s. Importing request fails with a ModuleNotFoundError.
Q5
C — Without brackets, data holds the json method instead of the response data. It should be r.json() to call the method.