Slack alert on uncaught exception in main()#97
Open
MilesMorel wants to merge 1 commit intomasterfrom
Open
Conversation
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>
| 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 }} |
Collaborator
There was a problem hiding this comment.
Maybe we should have a dev slack webhook so we can distinguish between dev and prod errors
| raise | ||
|
|
||
|
|
||
| def notify_slack_of_exception(tb_text): |
Collaborator
There was a problem hiding this comment.
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)
| 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.") |
Collaborator
There was a problem hiding this comment.
should we print the traceback in this case? We'll still want to see the error
| raise | ||
|
|
||
|
|
||
| def notify_slack_of_exception(tb_text): |
Collaborator
There was a problem hiding this comment.
Suggested change
| def notify_slack_of_exception(tb_text): | |
| def notify_slack_of_exception(traceback_text): |
| return | ||
|
|
||
| workflow = os.environ.get("GITHUB_WORKFLOW", "local run") | ||
| repo = os.environ.get("GITHUB_REPOSITORY", "") |
Collaborator
There was a problem hiding this comment.
Suggested change
| repo = os.environ.get("GITHUB_REPOSITORY", "") | |
| repo = os.environ.get("GITHUB_REPOSITORY") |
| header = f"CutePetsBoston run failed in *{workflow}*" | ||
| if run_link: | ||
| header += f" (<{run_link}|view run>)" | ||
| text = f"{header}\n```{tb_text.strip()[-2500:]}```" |
Collaborator
There was a problem hiding this comment.
can we put 2500 in a constant variable and also leave a comment on why that number was chosen?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
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.notify_slack_of_exceptionwhich posts toSLACK_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.SLACK_WEBHOOK_URLrepo secret into bothdev.ymlandprod.ymlworkflows.Closes #74
Setup (already done outside this PR)
#cutepetsboston-alertsSlack channel created with an Incoming Webhook.SLACK_WEBHOOK_URLadded as a repo secret in GitHub.Test plan
SLACK_WEBHOOK_URLand forcing an exception — message appeared in#cutepetsboston-alertswith the traceback.tests/test_main.pystill passes.workflow_dispatchagainst a branch that intentionally raises).🤖 Generated with Claude Code