Skip to content

Commit

Permalink
Merge pull request #56 from anjackson/master
Browse files Browse the repository at this point in the history
Handle update exceptions properly, as per #55
  • Loading branch information
anjackson authored Jul 3, 2023
2 parents cc9f9b0 + 597ccd3 commit 16219eb
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions stat-pusher/script/stat_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import ast
import requests
from urllib.error import HTTPError
import sys
import dateutil.parser
import datetime

Expand All @@ -31,10 +30,12 @@ def get_json_value(uri, match):
response = r.json()
except HTTPError as he:
logger.error(f"HTTP error trying to get [{uri}]\n[{he}]")
sys.exit()
# Pass exception up the chain so the parent can continue to the next check:
raise he
except Exception as e:
logger.error(f"Failed to get [{uri}]\n[{e}]")
sys.exit()
# Pass exception up the chain so the parent can continue to the next check:
raise e

# extract value
value = response
Expand All @@ -44,8 +45,7 @@ def get_json_value(uri, match):
elif k in value[0]:
value = value[0][k]
else:
logger.error(f"match key [{k}] not found in uri {uri}\njson [{value}]")
sys.exit()
raise Exception(f"match key [{k}] not found in uri {uri}\njson [{value}]")
logger.debug(f"Value [{value}] type [{type(value)}]")

# ensure numerical value
Expand All @@ -56,13 +56,12 @@ def get_json_value(uri, match):
dt = dateutil.parser.parse(value)
except Exception as e:
logger.error(f"Value [{value}] type [{type(value)}] not recognised as datestamp")
sys.exit()
raise e
if isinstance(dt, datetime.datetime):
logger.debug(f"timestamp dt [{dt}] type [{type(dt)}]")
value = dt.timestamp()
logger.debug(f"Value epoch [{value}]")
else:
logger.error(f"Value [{value}] type [{type(value)}] not convertible to numeric")
sys.exit()
raise Exception(f"Value [{value}] type [{type(value)}] not convertible to numeric")

return value

0 comments on commit 16219eb

Please sign in to comment.