Always raise specific, contextual exception types instead of generic exceptions. Include relevant error details that help identify the root cause and potential solutions. This improves error tracking, debugging, and system reliability.
Always raise specific, contextual exception types instead of generic exceptions. Include relevant error details that help identify the root cause and potential solutions. This improves error tracking, debugging, and system reliability.
Bad:
if response.status_code != 200:
raise Exception("A non 200 HTTP status code was returned.")
if not data:
raise Exception("Invalid response from LLM")
Good:
if response.status_code != 200:
raise ValueError(
f"Seer API returned unexpected status code {response.status_code}: {response.text}"
)
if not data:
raise ValueError("LLM returned empty response when generating summary")
The specific exception type (e.g., ValueError, TypeError, KeyError) should match the error condition. Include relevant context like parameter values, system state, or error messages in the exception text. This helps with:
Enter the URL of a public GitHub repository