We got a serverless function and split the functionality in multiple smaller functions. One function transforms arrays of a dictionary in strings. This is because Azure Datafactory can't handle arrays with the build in copy activity. This is the start code:
def refactor_arrays(data, firma):
for index, item in enumerate(data):
data[index]['versionedRepresentations']['components']['1'] = str(
item['versionedRepresentations']['components']['1'])
data[index]['versionedRepresentations']['fixVersions']['1'] = str(
item['versionedRepresentations']['fixVersions']['1'])
data[index]['versionedRepresentations']['labels']['1'] = str(
item['versionedRepresentations']['labels']['1'])
data[index]['versionedRepresentations']['customfield_10005']['2'] = str(
item['versionedRepresentations']['customfield_10005']['2'])
return data
This is how I as a bginner would handle errors.
def refactor_arrays(data, firma):
for index, item in enumerate(data):
try:
data[index]['versionedRepresentations']['components']['1'] = str(
item['versionedRepresentations']['components']['1'])
except KeyError:
logging.info(
'Key does not exist. Most likely another API endpoint gets called.')
except:
logging.error(
'Something went wrong when trying to convert an array to a string.')
try:
data[index]['versionedRepresentations']['fixVersions']['1'] = str(
item['versionedRepresentations']['fixVersions']['1'])
except KeyError:
logging.info(
'Key does not exist. Most likely another API endpoint gets called.')
except:
logging.error(
'Something went wrong when trying to convert an array to a string.')
try:
data[index]['versionedRepresentations']['labels']['1'] = str(
item['versionedRepresentations']['labels']['1'])
except KeyError:
logging.info(
'Key does not exist. Most likely another API endpoint gets called.')
except:
logging.error(
'Something went wrong when trying to convert an array to a string.')
try:
data[index]['versionedRepresentations']['customfield_10005']['2'] = str(
item['versionedRepresentations']['customfield_10005']['2'])
except KeyError:
logging.info(
'Key does not exist. Most likely another API endpoint gets called.')
except:
logging.error(
'Something went wrong when trying to convert an array to a string.')
return data
try catchblock for whole function, no need to puttry catchfor every statement