Start learning free

LESSON 46 OF 107 · DATA ENGINEERING: DATA QUALITY

Checks That Stop the Run in Python

Practice

A check that only prints FAIL protects nothing: the pipeline goes on, builds the report from the bad rows, and the dashboard shows wrong numbers before anyone reads the log. A real pipeline stops the run when a check fails. The report isn't rebuilt, so yesterday's report stays in place: a day old, but never wrong. Schedulers such as Airflow do this for you (a failed task stops the tasks after it); here you build it in plain Python.

raise, try and except: raise ValueError("message") stops the function at once, and every function that called it, until a try block catches the error. Its except ValueError as error: part runs instead of a crash, and str(error) gives the message back. For example:

def check_age(age):
    if age < 0:
        raise ValueError("age is negative")
    print("age: PASS")

try:
    check_age(-3)
    print("done")
except ValueError as error:
    print("stopped: " + str(error))

This prints only stopped: age is negative: the PASS line and done never run.

The starting code has load_staging(), which loads the week's files into stg_orders; build_report(), which rewrites report.csv with the newest day and the number of paid orders; two check queries; and check(name, query), which counts the rows that break a rule. Before your code runs, report.csv holds yesterday's report. On the week's files every status is known, but order 5058 has a blank customer_id, so your run must stop before the report. Then the lesson's test shows report.csv, fills in the customer of order 5058 and runs your pipeline again. The output must be:

status is known: PASS
run stopped by check: customer_id is not null
up_to | paid_orders
2026-05-05 | 50
(1 row)
Next run, with the customer of order 5058 filled in:
status is known: PASS
customer_id is not null: PASS
report.csv rebuilt
up_to | paid_orders
2026-05-07 | 67
(1 row)

Your task: make check raise a ValueError with the check's name when more than 0 rows break its rule, call both checks in run_pipeline between loading and building the report, and run the pipeline inside a try whose except prints run stopped by check: followed by the check's name.


Assignment
Follow the Coding Tutorial to make a failed check raise, call the checks between loading and the report, and catch the error in the main part.


Hint
The raise goes inside the if, above the PASS line, so a failed check never prints PASS. The code can't run while the try has no except yet: type all four lines of the main part before you expect output.


STEP 1 OF 8
    Ctrl + Enter to check

    De Dq Stop The Run in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free