Sources on the network fail now and then: the shop's server restarts, a connection drops. When a call gets no answer, Python raises a ConnectionError. If nothing catches it, the whole run stops and the day stays missing until someone runs it again by hand, though the same call would have worked a minute later. A retry calls the source again a few times before it gives up.
Catching an error: code inside try: may raise an error. An except SomeError: block right below it runs instead of stopping the program, and the program goes on. Catch only the error you expect, so a real bug still stops the run with its own message:
for text in ["3", "two", "5"]:
try:
print(int(text))
except ValueError:
print(text, "is not a number")
A retry also waits between tries. time.sleep(seconds) pauses the program, and the wait grows after each failure (5 s, then 10 s, then 15 s), which is called backoff: the server gets time to come back. Here WAIT_SECONDS is 0, so the check stays fast. And a retry must end: after the last try, raise ConnectionError("…") stops the run with a clear error instead of passing nothing on to transform. Schedulers such as Airflow offer retries as a setting on each task; here you build the idea yourself.
The hidden code gives you fetch_orders(run_date): it fails twice with ConnectionError, then returns the day's orders. Your script runs 2026-05-01 and must print:
Attempt 1 failed Attempt 2 failed Loaded 2026-05-01: 10 rows
Then the check makes the source fail 5 times in a row and runs 2026-05-02: your extract must try 3 times, then stop the run with ConnectionError, so nothing is loaded for that day.
Your task: write extract(run_date). Loop over the attempts 1 to 3; in a try, return what fetch_orders(run_date) gives; on a ConnectionError, print Attempt N failed and wait WAIT_SECONDS * attempt seconds; after the loop, raise a ConnectionError.
Assignment
Follow the Coding Tutorial to write extract with a retry loop around fetch_orders.
Hintreturn inside the loop ends the function as soon as a try works. The raise goes after the loop, at the level of for: the code only gets there when all three tries failed.
Free plan: 30 tutor messages. PRO is unlimited.
