Skip to content

Improve exception handling to avoid broad exception catching#133

Merged
psykzz merged 2 commits intomainfrom
claude/improve-exception-handling
Mar 28, 2026
Merged

Improve exception handling to avoid broad exception catching#133
psykzz merged 2 commits intomainfrom
claude/improve-exception-handling

Conversation

@Claude
Copy link
Copy Markdown
Contributor

@Claude Claude AI commented Mar 28, 2026

Multiple cogs used overly broad except Exception handlers that swallowed errors and made debugging difficult. Some handlers silently suppressed all exceptions with bare pass statements, providing no indication when failures occurred.

Changes

empty_voices/api.py

  • Config cleanup (L74-77): Split broad exception into KeyError, ValueError for expected config errors, with log.exception() for unexpected cases
  • Category refresh (L127-136): Added discord.HTTPException handler with exc_info=True for HTTP-specific errors
  • Channel creation (L208-220): Separated discord.HTTPException from generic exceptions, added detailed error messages
  • Channel operations (L78-81, L239-241): Changed log.error() to log.exception() for full stack traces on unexpected errors

hat/hat.py

  • Message cleanup (L112-119, L126-133): Replaced bare except Exception: pass with specific discord.HTTPException handler and logging
  • Avatar fetching (L212-217): Added discord.HTTPException handler and proper error logging instead of silent return None

nw_server_status/server_status.py

  • Background task (L46-51): Split broad handler into httpx.HTTPError, httpx.TimeoutException for network errors
  • Data fetching (L69-76): Added KeyError, ValueError for parsing errors, separated from network errors
  • HTTP retry logic (L267-274): Replaced internal exception paths (httpx._exceptions) with public API, added per-attempt logging

Pattern

Before:

except Exception as e:
    log.error(f"Error: {e}")  # No stack trace, catches everything

After:

except discord.HTTPException as e:
    log.error(f"HTTP error: {e}", exc_info=True)  # Stack trace for known errors
except Exception as e:
    log.exception(f"Unexpected error: {e}")  # Full traceback for unknowns

All changes maintain backward compatibility while significantly improving error visibility and debuggability.

- empty_voices/api.py: Replace broad Exception handlers with specific discord.HTTPException, KeyError, ValueError
- hat/hat.py: Replace bare Exception handlers with specific discord.HTTPException and add logging
- nw_server_status/server_status.py: Replace broad Exception with specific httpx.HTTPError and httpx.TimeoutException
- All changes use log.exception() for full tracebacks and exc_info=True for better debugging

Agent-Logs-Url: https://github.com/psykzz/cogs/sessions/3bb4ba79-1e3b-4484-a5ac-3c0257f5d452

Co-authored-by: psykzz <1134201+psykzz@users.noreply.github.com>
@Claude Claude AI changed the title [WIP] Fix broad exception catching in multiple cogs Improve exception handling to avoid broad exception catching Mar 28, 2026
@Claude Claude AI requested a review from psykzz March 28, 2026 10:57
@psykzz psykzz marked this pull request as ready for review March 28, 2026 11:13
@psykzz psykzz merged commit 4cb65e4 into main Mar 28, 2026
2 checks passed
@psykzz psykzz deleted the claude/improve-exception-handling branch March 28, 2026 11:13
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.

🔧 Improve exception handling to avoid broad exception catching

2 participants