*** Modified based on suggestions. But where i'm struggling is to find the right way to check if an exception occurred and append to the saved exception list. As the function has return value, None check doesn't work. Any suggestions with the modified code? ****
I'm building a python application. The Application has two functions - Make a series of API calls based on parameter and write the response to a file. It works fine when it encounters a handled exceptions and continues executing the rest of the code.
But what I really want is to save the exception details to a variable, continue with the execution and at the end of the program, check if any exception was caught, print it and exit with 1. An external monitoring app will do some post processing with this data.
I have tried it, but it's not working as intended. Any suggestions on how to make it work?
import traceback, sys
import requests
import sys
import logging
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
# logging.basicConfig(level=logging.DEBUG)
save_exc_info = [] # declare a variable to save an exception
def api_test(endpoint):
try:
print('Starting the API trigger')
headers = {'Connection': 'keep-alive'}
session = requests.Session()
retries = Retry(total=2, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["GET", "POST"])
session.mount('https://', HTTPAdapter(max_retries=retries))
response = session.get(endpoint, timeout=3, headers=headers)
response.raise_for_status()
print('Job completed successfully')
return response # return value from function
except requests.exceptions.HTTPError as e:
print('HTTP error occurred while making the API' + str(traceback.format_exc()))
return e # return exception as e
except requests.exceptions.ConnectionError as e:
print('Connection error occurred while making the API call' + str(traceback.format_exc()))
return e # return exception as e
def write_response_to_file(file, endpoint):
data_from_api = api_test(endpoint) # second function calls the first to get the API response
try:
if data_from_api.text:
with open(file, 'a') as file_to_write:
file_to_write.write(data_from_api.text)
print('Successful write to local')
else:
with open(file, 'w'):
print('write empty file')
except (IOError, OSError) as e:
print('Failure occurred while writing the API data')
return e # return exception as e
file_location = 'C:\\localpath\\test.json'
api_trigger = [1, 2, 3]
for triggers in api_trigger:
service = 'https://postman-echo.com/delay/' + str(triggers)
excep = write_response_to_file(file_location, service)
if excep: # What is the correct way to check for exception and append to saved exception list
save_exc_info.append(excep)
if save_exc_info: # Check if exception occurred
print('Failure occurred during execution') # print msg and the actual exception
print(save_exc_info)
sys.exit(1) # Final system exit code 1 if any exception occurred