Skip to content

Commit ca296ed

Browse files
committed
refactor(error_handling): enhance Sentry integration for improved exception tracking
- Added `capture_exception_safe` calls in the Starboard and StatusRoles modules to log exceptions with additional context, improving observability during error occurrences. - Updated error handling in the Wiki module to capture API errors with specific endpoint context. - Streamlined exception logging across various operations, ensuring that unexpected errors are reported to Sentry while maintaining appropriate logging levels for expected behaviors.
1 parent 3d0b289 commit ca296ed

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

src/tux/modules/features/starboard.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from tux.core.bot import Tux
1616
from tux.core.checks import requires_command_permission
1717
from tux.core.converters import get_channel_safe
18+
from tux.services.sentry import capture_exception_safe
1819
from tux.ui.embeds import EmbedCreator, EmbedType
1920

2021

@@ -191,6 +192,13 @@ async def setup_starboard(
191192

192193
except Exception as e:
193194
logger.error(f"Error configuring starboard: {e}")
195+
capture_exception_safe(
196+
e,
197+
extra_context={
198+
"operation": "configure_starboard",
199+
"guild_id": str(ctx.guild.id) if ctx.guild else None,
200+
},
201+
)
194202
await ctx.send(f"An error occurred while configuring the starboard: {e}")
195203

196204
@starboard.command(
@@ -236,6 +244,13 @@ async def remove_starboard(self, ctx: commands.Context[Tux]) -> None:
236244

237245
except Exception as e:
238246
logger.error(f"Error removing starboard configuration: {e}")
247+
capture_exception_safe(
248+
e,
249+
extra_context={
250+
"operation": "remove_starboard_config",
251+
"guild_id": str(ctx.guild.id) if ctx.guild else None,
252+
},
253+
)
239254
await ctx.send(
240255
f"An error occurred while removing the starboard configuration: {e}",
241256
)
@@ -279,6 +294,15 @@ async def get_existing_starboard_message(
279294

280295
except Exception as e:
281296
logger.error(f"Error while fetching starboard message: {e}")
297+
capture_exception_safe(
298+
e,
299+
extra_context={
300+
"operation": "get_existing_starboard_message",
301+
"original_message_id": str(original_message.id)
302+
if original_message
303+
else None,
304+
},
305+
)
282306

283307
return None
284308

@@ -357,6 +381,15 @@ async def create_or_update_starboard_message(
357381

358382
except Exception as e:
359383
logger.error(f"Error while creating or updating starboard message: {e}")
384+
capture_exception_safe(
385+
e,
386+
extra_context={
387+
"operation": "create_or_update_starboard_message",
388+
"channel_id": str(starboard_channel.id)
389+
if starboard_channel
390+
else None,
391+
},
392+
)
360393

361394
async def handle_starboard_reaction(
362395
self,
@@ -415,6 +448,14 @@ async def handle_starboard_reaction(
415448

416449
except Exception as e:
417450
logger.error(f"Unexpected error in handle_starboard_reaction: {e}")
451+
capture_exception_safe(
452+
e,
453+
extra_context={
454+
"operation": "handle_starboard_reaction",
455+
"guild_id": str(payload.guild_id) if payload.guild_id else None,
456+
"message_id": str(payload.message_id),
457+
},
458+
)
418459

419460
async def handle_reaction_clear(
420461
self,
@@ -462,6 +503,14 @@ async def handle_reaction_clear(
462503

463504
except Exception as e:
464505
logger.error(f"Error in handle_reaction_clear: {e}")
506+
capture_exception_safe(
507+
e,
508+
extra_context={
509+
"operation": "handle_reaction_clear",
510+
"guild_id": str(payload.guild_id) if payload.guild_id else None,
511+
"message_id": str(payload.message_id),
512+
},
513+
)
465514

466515

467516
async def setup(bot: Tux) -> None:

src/tux/modules/features/status_roles.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from tux.core.base_cog import BaseCog
1515
from tux.core.bot import Tux
16+
from tux.services.sentry import capture_exception_safe
1617
from tux.shared.config import CONFIG
1718

1819

@@ -154,15 +155,28 @@ async def check_and_update_roles(self, member: discord.Member):
154155
await member.remove_roles(role)
155156

156157
except re.error:
157-
logger.exception(
158+
# Configuration error - don't send to Sentry
159+
logger.warning(
158160
f"Invalid regex pattern '{pattern}' in STATUS_ROLES config",
159161
)
160162
except discord.Forbidden:
161-
logger.exception(
163+
# User error (permission denied) - don't send to Sentry
164+
logger.warning(
162165
f"Bot lacks permission to modify roles for {member.display_name} in {member.guild.name}",
163166
)
164-
except Exception:
165-
logger.exception(f"Error updating roles for {member.display_name}")
167+
except Exception as e:
168+
# Unexpected error - send to Sentry
169+
logger.error(f"Error updating roles for {member.display_name}: {e}")
170+
171+
capture_exception_safe(
172+
e,
173+
extra_context={
174+
"operation": "update_status_roles",
175+
"member_id": str(member.id),
176+
"guild_id": str(member.guild.id),
177+
"pattern": pattern,
178+
},
179+
)
166180

167181

168182
async def setup(bot: Tux) -> None:

src/tux/modules/moderation/cases.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,9 @@ async def _resolve_user(self, user_id: int) -> discord.User | MockUser:
513513
logger.warning(f"Could not find user with ID {user_id}")
514514
return MockUser(user_id)
515515
except Exception as e:
516-
logger.exception(f"Error resolving user with ID {user_id}: {e}")
516+
# Graceful fallback - don't use exception() level for expected fallback behavior
517+
logger.warning(f"Error resolving user with ID {user_id}: {e}")
518+
# Don't send to Sentry - this is expected fallback behavior
517519
return MockUser(user_id)
518520

519521
async def _resolve_moderator(self, moderator_id: int) -> discord.User | MockUser:

src/tux/modules/tools/tldr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from tux.core.base_cog import BaseCog
1818
from tux.core.bot import Tux
1919
from tux.core.flags import TldrFlags
20+
from tux.services.sentry import capture_exception_safe
2021
from tux.services.wrappers.tldr import SUPPORTED_PLATFORMS, TldrClient
2122
from tux.shared.functions import generate_usage
2223
from tux.ui.embeds import EmbedCreator
@@ -92,6 +93,17 @@ async def _initialize_cache_async(self):
9293
f"TLDR Cog: Exception during cache update for '{lang_code}': {e}",
9394
exc_info=True,
9495
)
96+
# Capture exception with context for Sentry
97+
capture_exception_safe(
98+
e,
99+
extra_context={
100+
"tldr_cache_update": {
101+
"language": lang_code,
102+
"operation": "cache_update",
103+
"cache_needs_update": True,
104+
},
105+
},
106+
)
95107
else:
96108
logger.debug(
97109
f"TLDR Cog: Cache for '{lang_code}' is recent, skipping update.",

src/tux/modules/utility/wiki.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from tux.core.base_cog import BaseCog
1313
from tux.core.bot import Tux
1414
from tux.services.http_client import http_client
15+
from tux.services.sentry import capture_api_error
1516
from tux.ui.embeds import EmbedCreator
1617

1718

@@ -116,6 +117,8 @@ async def query_wiki(self, base_url: str, search_term: str) -> tuple[str, str]:
116117
return title, url
117118
except Exception as e:
118119
logger.error(f"Wiki API request failed: {e}")
120+
121+
capture_api_error(e, endpoint="wiki_api")
119122
return "error", "error"
120123

121124
return "error", "error"

0 commit comments

Comments
 (0)