Skip to content

Commit d5579a0

Browse files
committed
fix: catch AttributeError exception when serializing response as json
1 parent cbfff97 commit d5579a0

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/main.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,25 @@ def get_and_update_historic_usage(
7171
tuple: A tuple containing the updated historic usage data and a list of dates added.
7272
"""
7373
# Get the usage data
74-
api_response = gh.get(f"/orgs/{org}/copilot/metrics/reports/organization-28-day/latest").json()
75-
usage_data = requests.get(api_response["download_links"][0], timeout=30).json()["day_totals"]
74+
try:
75+
api_response = gh.get(f"/orgs/{org}/copilot/metrics/reports/organization-28-day/latest")
76+
api_response_json = api_response.json()
77+
except AttributeError as e:
78+
logger.error("Error getting usage data: %s", api_response)
79+
return [], []
7680

81+
usage_data = requests.get(api_response_json["download_links"][0], timeout=30).json()["day_totals"]
7782
logger.info("Usage data retrieved")
7883

84+
# Get the existing historic usage data from S3
7985
try:
8086
response = s3.get_object(Bucket=BUCKET_NAME, Key=OBJECT_NAME)
8187
historic_usage = json.loads(response["Body"].read().decode("utf-8"))
8288
except ClientError as e:
8389
logger.error("Error getting %s: %s. Using empty list.", OBJECT_NAME, e)
8490
historic_usage = []
8591

92+
# Append the new usage data to the existing historic usage data
8693
dates_added = []
8794

8895
for day in usage_data:
@@ -94,7 +101,7 @@ def get_and_update_historic_usage(
94101
sorted_historic_usage = sorted(historic_usage, key=lambda x: x["day"])
95102

96103
if not write_data_locally:
97-
# Write the updated historic_usage to historic_usage_data.json
104+
# Write the updated historic_usage to organisation_history.json
98105
update_s3_object(s3, BUCKET_NAME, OBJECT_NAME, sorted_historic_usage)
99106
else:
100107
local_path = f"output/{OBJECT_NAME}"
@@ -255,7 +262,7 @@ def handler(event: dict, context) -> str: # pylint: disable=unused-argument, to
255262
historic_usage, dates_added = get_and_update_historic_usage(s3, gh, write_data_locally)
256263

257264
logger.info(
258-
"Process complete",
265+
"Process finished",
259266
extra={
260267
"bucket": BUCKET_NAME,
261268
"no_days_added": len(dates_added),

0 commit comments

Comments
 (0)