Search code examples
pythonzapier

Code By Zapier: Python - How to make it return error


I have written some python code to fetch a http request using requests library.

How do I make it error out if, after examining response, I figure out that I have not gotten the desired data?

Please note that I want to raise error only in certain cases.

Right now, I return an output object and the step always shows a passed.


Solution

  • David here, from the Zapier Platform team.

    There's no secret sauce for Python on Zapier - If you want to raise an error, you can just raise it. How do you that depends on what situations you want to trigger an error state.

    response = requests.get(url)
    
    # raises for all error codes >= 400
    response.raise_for_status() 
    result = response.json()
    
    # or more manually, for specific cases
    if result['my_key'] != 'key_i_want':
        raise Exception('bad key')
    

    Does that make sense? ​Let me know if you've got any other questions!