'NoneType stopping the script with nested functions

I am trying to achieve the following:

  1. Run a script that will upon error call another script.
  2. Get the output of that data and insert it into the existing script to update the token value
  3. Then display the "data"
  4. If the value of x is not invalid it will just display the "data"

Here is the error I get when the value is not invalid. I do understand why I am getting this error but I'm having an issue handling the script and making it go to the next step

    if x not in err_data: TypeError: argument of type 'NoneType' is not iterable
url = 'https://somedomain.com/blah/etc'

headers = {
        'Content-Type': 'application/json',
        'Authorization': apikey
}
def check_token():
    x = 'invalid_token'
    response = requests.get(url, headers=headers)
    data = response.json()
    err_data = data.get('error')
    #When I get a response "None" I get the above mentioned error and script stops
    if x in err_data:
        refresh_token.ref_token()
        #If that value of x is present run this script that I imported
    else:
        check_data()
        #If the value of x is "None" run this script

def check_data():
    response = requests.get(url, headers=headers)
    data = response.json()
    pp(data)

if __name__ == "__main__":
    check_token()
    check_data()
    refresh_token.ref_token()

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source