Skip to content

Commit

Permalink
Additional error handling for non-fatal errors in notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Sep 25, 2024
1 parent e522800 commit c9f1e57
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Next version

### 🔧 Fixed

* Add additional error handling for non-fatal errors during notifications.


## 0.1.4 - 2024-09-24

### ✨ Improved
Expand Down
48 changes: 28 additions & 20 deletions src/lvmcryo/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,24 @@ async def post_to_slack(

failed: bool = False
for channel in channels:
async with httpx.AsyncClient(follow_redirects=True) as client:
response = await client.post(
route,
json={
"channel": channel,
"text": text,
"username": self.config.slack_from,
"mentions": mentions,
},
)
try:
async with httpx.AsyncClient(follow_redirects=True) as client:
response = await client.post(
route,
json={
"channel": channel,
"text": text,
"username": self.config.slack_from,
"mentions": mentions,
},
)

if response.status_code != 200:
warnings.warn(f"Failed sending message to Slack: {response.text}")
failed = True

if response.status_code != 200:
warnings.warn(f"Failed sending message to Slack: {response.text}")
except Exception as ee:
warnings.warn(f"Failed sending message to Slack: {ee}")
failed = True

return not failed
Expand Down Expand Up @@ -280,6 +285,10 @@ async def notify_after_fill(
new_line = pattern.sub(r"\1\2", line)
log_lines.append(new_line)

lvmweb_url: str | None = None
if record_pk is not None:
lvmweb_url = self.config.lvmweb_fill_url.format(fill_id=record_pk)

if post_to_slack:
try:
if success:
Expand All @@ -302,17 +311,15 @@ async def notify_after_fill(
level=NotificationLevel.error,
)

if lvmweb_url:
await self.post_to_slack(
"Information about the fill can be found at "
f"<{lvmweb_url}|this link>."
)

except Exception as err:
log.error(f"Failed posting to Slack: {err!r}")

lvmweb_url: str | None = None
if record_pk is not None:
lvmweb_url = self.config.lvmweb_fill_url.format(fill_id=record_pk)
await self.post_to_slack(
"Information about the fill can be found at "
f"<{lvmweb_url}|this link>."
)

if send_email:
subject: str = (
"SUCCESS: LVM LN2 fill completed"
Expand Down Expand Up @@ -402,6 +409,7 @@ async def notify_after_fill(
except Exception as err:
log.error(f"Failed rendering template: {err!r}")
log.warning("Sending a plain text message.")
html_message = None

self.send_email(
subject=subject,
Expand Down

0 comments on commit c9f1e57

Please sign in to comment.