Skip to content

Slack alert on uncaught exception in main()#97

Open
MilesMorel wants to merge 1 commit intomasterfrom
slack-alert-on-failure
Open

Slack alert on uncaught exception in main()#97
MilesMorel wants to merge 1 commit intomasterfrom
slack-alert-on-failure

Conversation

@MilesMorel
Copy link
Copy Markdown
Collaborator

Summary

  • Wraps main() in a try/except so any uncaught exception is caught, the traceback is posted to Slack, and then re-raised so the GitHub Actions run still fails.
  • Adds notify_slack_of_exception which posts to SLACK_WEBHOOK_URL (no-op if unset), including the workflow name and a link to the GitHub Actions run when those env vars are present. Truncates the traceback to the last 2500 chars to stay well under Slack's message size limit.
  • Wires the new SLACK_WEBHOOK_URL repo secret into both dev.yml and prod.yml workflows.

Closes #74

Setup (already done outside this PR)

  • #cutepetsboston-alerts Slack channel created with an Incoming Webhook.
  • SLACK_WEBHOOK_URL added as a repo secret in GitHub.

Test plan

  • Verified locally by setting SLACK_WEBHOOK_URL and forcing an exception — message appeared in #cutepetsboston-alerts with the traceback.
  • Existing tests/test_main.py still passes.
  • After merge, confirm a real failure in the scheduled run posts to Slack (or trigger one via workflow_dispatch against a branch that intentionally raises).

🤖 Generated with Claude Code

Wraps main() in try/except and posts the traceback to a
SLACK_WEBHOOK_URL (passed via GitHub Actions secret) so failures
in the scheduled run surface in #cutepetsboston-alerts. Re-raises
the exception so the workflow still fails.

Closes #74

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Comment thread .github/workflows/dev.yml
INSTAGRAM_PAGE_ACCESS_TOKEN: ${{ secrets.INSTAGRAM_PAGE_ACCESS_TOKEN }}
BLUESKY_HANDLE: ${{ secrets.BLUESKY_HANDLE }}
BLUESKY_PASSWORD: ${{ secrets.BLUESKY_PASSWORD }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should have a dev slack webhook so we can distinguish between dev and prod errors

Comment thread main.py
raise


def notify_slack_of_exception(tb_text):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's put the notify_slack_of_exception below the run function just so that the function definition order matches the order they are called in the main function (this makes onboarding people onto the slightly easier)

Comment thread main.py
def notify_slack_of_exception(tb_text):
webhook_url = os.environ.get("SLACK_WEBHOOK_URL")
if not webhook_url:
print("SLACK_WEBHOOK_URL not set; skipping Slack alert.")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we print the traceback in this case? We'll still want to see the error

Comment thread main.py
raise


def notify_slack_of_exception(tb_text):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def notify_slack_of_exception(tb_text):
def notify_slack_of_exception(traceback_text):

Comment thread main.py
return

workflow = os.environ.get("GITHUB_WORKFLOW", "local run")
repo = os.environ.get("GITHUB_REPOSITORY", "")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
repo = os.environ.get("GITHUB_REPOSITORY", "")
repo = os.environ.get("GITHUB_REPOSITORY")

Comment thread main.py
header = f"CutePetsBoston run failed in *{workflow}*"
if run_link:
header += f" (<{run_link}|view run>)"
text = f"{header}\n```{tb_text.strip()[-2500:]}```"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put 2500 in a constant variable and also leave a comment on why that number was chosen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a way to the github action to slack when an uncaught exception happens in a github action

2 participants