Skip to content

Commit

Permalink
handling error when message not sent
Browse files Browse the repository at this point in the history
  • Loading branch information
burnout87 committed Sep 28, 2023
1 parent 51d1bf6 commit 9a6600f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions cdci_data_analysis/analysis/matrix_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
num_msgs_sending_max_tries = 5
msg_sending_retry_sleep_s = .5

class MatrixMsgNotSent(BadRequest):
class MatrixMessageNotSent(BadRequest):
pass


Expand All @@ -33,7 +33,7 @@ def timestamp2isot(timestamp_or_string: typing.Union[str, float]):
timestamp_or_string = validate_time(timestamp_or_string).strftime("%Y-%m-%d %H:%M:%S")
except (ValueError, OverflowError, TypeError, OSError) as e:
logger.warning(f'Error when constructing the datetime object from the timestamp {timestamp_or_string}:\n{e}')
raise MatrixMsgNotSent(f"Matrix message not sent: {e}")
raise MatrixMessageNotSent(f"Matrix message not sent: {e}")

return timestamp_or_string

Expand Down Expand Up @@ -171,8 +171,27 @@ def send_message(

res = requests.post(url, json=message_data, headers=headers)

if res.status_code not in [200, 201, 204]:
try:
response_json = res.json()
error_msg = response_json['error']
except json.decoder.JSONDecodeError:
error_msg = res.text
logger.warning(f"there seems to be some problem in sending a message via matrix:\n"
f"the requested url {url} lead to the error {error_msg}, "
"this might be due to an error in the url or the page requested no longer exists, "
"please check it and try to issue again the request")
matrix_error_message = error_msg

sentry.capture_message(f'issue in sending a message via matrix, the requested url {url} lead to the error '
f'{matrix_error_message}')
raise MatrixMessageNotSent('issue when performing a request to the product gallery',
status_code=res.status_code,
payload={'matrix_error_message': matrix_error_message})

logger.info("Message successfully sent")


return message_data


Expand Down
2 changes: 1 addition & 1 deletion cdci_data_analysis/flask_app/dispatcher_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1862,7 +1862,7 @@ def run_query(self, off_line=False, disp_conf=None):
scratch_dir=self.scratch_dir)

query_out.set_status_field('matrix_message_status', 'matrix message sent')
except matrix_helper.MatrixMsgNotSent as e:
except matrix_helper.MatrixMessageNotSent as e:
query_out.set_status_field('matrix_message_status', 'sending matrix message failed')
logging.warning(f'matrix message sending failed: {e}')
sentry.capture_message(f'sending matrix message failed {e.message}')
Expand Down

0 comments on commit 9a6600f

Please sign in to comment.