open-notebook/open_notebook/utils/error_classifier.py
Luis Novo 37217b01de
fix: error hints, Italian in the language menu and dev fallbacks (docs review) (#1457)
* fix: error hints, Italian menu entry and dev fallbacks found by the docs review

- error_classifier: read timeouts get their own message naming
  ESPERANTO_LLM_TIMEOUT; connect failures keep "Could not connect".
  Auth hint points to Manage -> Models; provision's "Please go to
  Manage" message is recognized again.
- Podcast voice hint and the empty-episodes text point to real screens.
- LanguageToggle renders the registered languages list, so it-IT shows
  up (and future locales can't be missed); it-IT date locale added.
  The per-language common.* labels it used are removed.
- make start-all uses the shipped compose file.
- Legacy SURREAL_ADDRESS/SURREAL_PORT fallback builds ws://host:port/rpc.
- .env.example uses the real OPEN_NOTEBOOK_CHUNK_* names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: address review (IPv6 legacy address, connection-timed-out wording, stronger tests)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: assert exception class for OS connection timeouts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 22:51:31 -03:00

130 lines
4.3 KiB
Python

"""
Error classification utility for LLM provider errors.
Maps raw exceptions from AI providers/Esperanto/LangChain to user-friendly
error messages and appropriate exception types.
"""
from loguru import logger
from open_notebook.exceptions import (
AuthenticationError,
ConfigurationError,
ContextLengthExceededError,
ExternalServiceError,
NetworkError,
OpenNotebookError,
RateLimitError,
)
# Classification rules: (keywords, exception_class, user_message or None to pass through)
_CLASSIFICATION_RULES: list[tuple[list[str], type[OpenNotebookError], str | None]] = [
# Authentication errors
(
["authentication", "unauthorized", "invalid api key", "invalid_api_key", "401"],
AuthenticationError,
"Authentication failed. Please check your API key in Manage -> Models.",
),
# Rate limit errors
(
["rate limit", "rate_limit", "429", "too many requests", "quota exceeded"],
RateLimitError,
"Rate limit exceeded. Please wait a moment and try again.",
),
# Model not found (pass through original message)
(
["model not found", "does not exist", "model_not_found"],
ConfigurationError,
None,
),
# Configuration errors from provision.py (pass through)
(
["no model configured", "please go to settings", "please go to manage"],
ConfigurationError,
None,
),
# Network errors (connect timeouts included: the provider was never reached)
(
[
"connecterror",
"connecttimeout",
"connection timed out",
"connection refused",
"connection error",
],
NetworkError,
"Could not connect to the AI provider. Please check your network connection and provider URL.",
),
# Read timeouts: the provider was reached but didn't answer within
# ESPERANTO_LLM_TIMEOUT (180 s unless set)
(
["timeoutexception", "timed out", "timeout"],
NetworkError,
"The AI provider took too long to respond. Try again, use a faster model, or raise ESPERANTO_LLM_TIMEOUT (180 seconds by default).",
),
# Context length errors
(
[
"context length",
"token limit",
"maximum context",
"context_length_exceeded",
"max_tokens",
],
ContextLengthExceededError,
"Content too large for the selected model. Try using a smaller selection or a model with a larger context window.",
),
# Payload too large errors
(
["413", "payload too large", "request entity too large"],
ExternalServiceError,
"The request payload is too large for the AI provider. Try reducing the content size or using a different model.",
),
# Provider availability errors
(
[
"500",
"502",
"503",
"service unavailable",
"overloaded",
"internal server error",
],
ExternalServiceError,
"The AI provider is temporarily unavailable. Please try again in a few minutes.",
),
]
def classify_error(exception: BaseException) -> tuple[type[OpenNotebookError], str]:
"""
Classify a raw exception into a user-friendly error type and message.
Args:
exception: Any exception from LLM providers/Esperanto/LangChain
Returns:
Tuple of (exception_class, user_friendly_message)
"""
error_str = str(exception).lower()
error_type_name = type(exception).__name__.lower()
combined = f"{error_type_name}: {error_str}"
for keywords, exc_class, message in _CLASSIFICATION_RULES:
for keyword in keywords:
if keyword in combined:
user_message = (
message if message is not None else _truncate(str(exception))
)
return exc_class, user_message
# Unclassified error - log for future improvement
logger.warning(f"Unclassified LLM error ({type(exception).__name__}): {exception}")
return ExternalServiceError, f"AI service error: {_truncate(str(exception))}"
def _truncate(text: str, max_length: int = 200) -> str:
"""Truncate text to max_length to avoid leaking verbose internal details."""
if len(text) <= max_length:
return text
return text[:max_length] + "..."