0

I am new to Airflow DAGs. I have a python script to fetch API calls. I have added a raise Exception in my code, if all API calls failed i.e. status_code!=200, then it should raise exception. However, in airflow logs, it is shown as INFO - [base] An error occurred: All API calls failed for all IDs and DAG succeeds. How do I make the DAG failed if all API calls fail.

Below is the sample code I have:

        response = requests.get(url)
        data = response.json()
        if response.status_code!=200:
            print(f"Error occurred for ID - Response code {response.status_code} {response.reason}")
            break
        if response.status_code==200:
            df1 = pd.concat([df1, pd.json_normalize(data)])
        else:
            print(f'No data to fetch data for {reaction}:{id}')


if not df1.empty:  
    all_accounts_failed = False

if all_accounts_failed:
    raise Exception("All API calls failed for all IDs")
    raise
else:
    try:
      #rest of the code

Expecting the DAG to be failed however DAG succeeded with just showing it as INFO

Snippet from DAG

1

1 Answer 1

0

There are two paths to solving this:

  1. You can update your code to raise AirflowException. Here is a link to Airflow Exceptions. You'll need to add to the imports from airflow.exceptions import AirflowException to use it. Raising an Airflow Exception will cause the DAG to fail.

  2. You can convert your requests to using the HttpHook, which will throw an AirflowException if the response is not a 200 response. Here is a link to the hook.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.