Skip to content

Commit af039b5

Browse files
committed
feat(error handling): add detailed error handling for code execution
Introduce new exceptions for code execution errors, including MissingCodeError, InvalidCodeFormatError, UnsupportedLanguageError, and CompilationError. These exceptions provide specific feedback to users and are integrated into the error handling configuration to log and optionally send certain errors to Sentry for monitoring. Enhancing error handling for code execution improves user experience by providing clear feedback on what went wrong and allows for better monitoring of issues related to code execution, aiding in debugging and improving system reliability.
1 parent 8a8a5c4 commit af039b5

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

tux/handlers/error.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323

2424
from tux.bot import Tux
2525
from tux.ui.embeds import EmbedCreator
26-
from tux.utils.exceptions import AppCommandPermissionLevelError, PermissionLevelError
26+
from tux.utils.exceptions import (
27+
AppCommandPermissionLevelError,
28+
CodeExecutionError,
29+
CompilationError,
30+
InvalidCodeFormatError,
31+
MissingCodeError,
32+
PermissionLevelError,
33+
UnsupportedLanguageError,
34+
)
2735

2836
# --- Constants and Configuration ---
2937

@@ -377,6 +385,32 @@ def _extract_missing_argument_details(error: Exception) -> dict[str, Any]:
377385
message_format="You need permission level `{error.permission}` to use this command.",
378386
send_to_sentry=False,
379387
),
388+
# === Code Execution Errors (from tux.utils.exceptions) ===
389+
MissingCodeError: ErrorHandlerConfig(
390+
message_format="{error}",
391+
log_level="INFO",
392+
send_to_sentry=False,
393+
),
394+
InvalidCodeFormatError: ErrorHandlerConfig(
395+
message_format="{error}",
396+
log_level="INFO",
397+
send_to_sentry=False,
398+
),
399+
UnsupportedLanguageError: ErrorHandlerConfig(
400+
message_format="{error}",
401+
log_level="INFO",
402+
send_to_sentry=False,
403+
),
404+
CompilationError: ErrorHandlerConfig(
405+
message_format="{error}",
406+
log_level="INFO",
407+
send_to_sentry=True, # Monitor frequency of compilation failures
408+
),
409+
CodeExecutionError: ErrorHandlerConfig(
410+
message_format="{error}",
411+
log_level="INFO",
412+
send_to_sentry=True, # Monitor general code execution issues
413+
),
380414
# === Discord API & Client Errors ===
381415
discord.ClientException: ErrorHandlerConfig(
382416
message_format="A client-side error occurred: {error}",

tux/utils/exceptions.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,60 @@ def __init__(self, service_name: str, status_code: int = 403):
115115
status_code,
116116
reason="API request failed due to insufficient permissions.",
117117
)
118+
119+
120+
# === Code Execution Exceptions ===
121+
122+
123+
class CodeExecutionError(Exception):
124+
"""Base exception for code execution errors."""
125+
126+
127+
class MissingCodeError(CodeExecutionError):
128+
"""Raised when no code is provided for execution."""
129+
130+
def __init__(self) -> None:
131+
super().__init__(
132+
"Please provide code with syntax highlighting in this format:\n"
133+
'```\n`\u200b``python\nprint("Hello, World!")\n`\u200b``\n```',
134+
)
135+
136+
137+
class InvalidCodeFormatError(CodeExecutionError):
138+
"""Raised when code format is invalid."""
139+
140+
def __init__(self) -> None:
141+
super().__init__(
142+
"Please provide code with syntax highlighting in this format:\n"
143+
'```\n`\u200b``python\nprint("Hello, World!")\n`\u200b``\n```',
144+
)
145+
146+
147+
class UnsupportedLanguageError(CodeExecutionError):
148+
"""Raised when the specified language is not supported."""
149+
150+
def __init__(self, language: str, supported_languages: list[str]) -> None:
151+
"""
152+
Initialize with language-specific error message.
153+
154+
Parameters
155+
----------
156+
language : str
157+
The unsupported language that was requested.
158+
supported_languages : list[str]
159+
List of supported language names.
160+
"""
161+
self.language = language
162+
self.supported_languages = supported_languages
163+
available_langs = ", ".join(supported_languages)
164+
165+
super().__init__(
166+
f"No compiler found for `{language}`. The following languages are supported:\n```{available_langs}```",
167+
)
168+
169+
170+
class CompilationError(CodeExecutionError):
171+
"""Raised when code compilation fails."""
172+
173+
def __init__(self) -> None:
174+
super().__init__("Failed to get output from the compiler. The code may have compilation errors.")

0 commit comments

Comments
 (0)