diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e816d1..30037e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/lvmcryo/notifier.py b/src/lvmcryo/notifier.py index 0de5b7e..665f74f 100644 --- a/src/lvmcryo/notifier.py +++ b/src/lvmcryo/notifier.py @@ -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 @@ -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: @@ -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" @@ -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,