Spaces:
Sleeping
Sleeping
File size: 12,647 Bytes
a1bf219 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
"""Comprehensive error handling for the trading analysis platform."""
from typing import Optional
class TradingAnalysisError(Exception):
"""Base exception for all trading analysis errors."""
def __init__(
self,
message: str,
details: Optional[str] = None,
suggestion: Optional[str] = None,
):
"""
Initialize error with comprehensive information.
Args:
message: The main error message
details: Additional technical details about the error
suggestion: User-friendly suggestion on how to resolve the issue
"""
self.message = message
self.details = details
self.suggestion = suggestion
super().__init__(self.format_error())
def format_error(self) -> str:
"""Format error message with all available information."""
parts = [f"β {self.message}"]
if self.details:
parts.append(f"\nDetails: {self.details}")
if self.suggestion:
parts.append(f"\nπ‘ Suggestion: {self.suggestion}")
return "\n".join(parts)
# Data Provider Errors
class DataProviderError(TradingAnalysisError):
"""Base class for data provider errors."""
pass
class TickerNotFoundError(DataProviderError):
"""Raised when a ticker symbol is not found."""
def __init__(self, ticker: str, provider: str = "data provider"):
super().__init__(
message=f"Ticker '{ticker}' not found",
details=f"The ticker symbol '{ticker}' could not be found in {provider}",
suggestion=(
f"Please verify the ticker symbol is correct. "
f"Examples: AAPL (stock), BTC-USD (crypto), GC=F (commodity), ^GSPC (index)"
),
)
class NoDataReturnedError(DataProviderError):
"""Raised when no data is returned for a valid ticker."""
def __init__(self, ticker: str, timeframe: str, provider: str = "data provider"):
super().__init__(
message=f"No data available for {ticker}",
details=f"No OHLC data returned for ticker '{ticker}' with timeframe '{timeframe}' from {provider}",
suggestion=(
f"This could mean: "
f"1) The market is closed and no recent data is available, "
f"2) The ticker doesn't support this timeframe, "
f"3) Try a different timeframe (e.g., '1d' instead of '{timeframe}')"
),
)
class RateLimitError(DataProviderError):
"""Raised when API rate limits are exceeded."""
def __init__(self, provider: str, wait_time: Optional[int] = None):
wait_msg = f" Please wait {wait_time} seconds." if wait_time else ""
super().__init__(
message=f"Rate limit exceeded for {provider}",
details=f"Too many requests to {provider} API.{wait_msg}",
suggestion=(
f"1) Wait a few minutes before trying again, "
f"2) Consider upgrading to a premium API key, "
f"3) Use a different data provider in Settings"
),
)
class InsufficientDataError(DataProviderError):
"""Raised when there's insufficient data for analysis."""
def __init__(self, ticker: str, required: int, actual: int):
super().__init__(
message=f"Insufficient data for analysis of {ticker}",
details=f"Need at least {required} data points for analysis, but only {actual} available",
suggestion=(
f"1) Try a longer time period (increase date range), "
f"2) Use a higher timeframe (e.g., '1d' instead of '1m'), "
f"3) Check if the asset has been trading long enough"
),
)
class DataValidationError(DataProviderError):
"""Raised when data validation fails."""
def __init__(self, issue: str):
super().__init__(
message="Data validation failed",
details=issue,
suggestion="This might be a temporary data provider issue. Try again in a few moments.",
)
# API and Configuration Errors
class APIKeyError(TradingAnalysisError):
"""Raised when API keys are missing or invalid."""
def __init__(self, provider: str):
super().__init__(
message=f"Missing or invalid API key for {provider}",
details=f"The {provider} API key is either not configured or invalid",
suggestion=(
f"1) Set your {provider} API key in the .env file or API Keys tab, "
f"2) Verify the API key is valid and active, "
f"3) Restart the application after setting the key"
),
)
class ConfigurationError(TradingAnalysisError):
"""Raised when configuration is invalid."""
def __init__(self, parameter: str, issue: str):
super().__init__(
message=f"Invalid configuration for {parameter}",
details=issue,
suggestion=f"Check the Settings tab and verify {parameter} is configured correctly",
)
# LLM Errors
class LLMError(TradingAnalysisError):
"""Base class for LLM-related errors."""
pass
class LLMProviderError(LLMError):
"""Raised when LLM provider encounters an error."""
def __init__(self, provider: str, model: str, error: str):
super().__init__(
message=f"{provider} API error",
details=f"Model '{model}' returned error: {error}",
suggestion=(
f"1) Check your {provider} API key is valid and has credits, "
f"2) Verify you have access to the '{model}' model, "
f"3) Try a different model in Settings, "
f"4) Check your internet connection"
),
)
class LLMTimeoutError(LLMError):
"""Raised when LLM request times out."""
def __init__(self, agent: str, timeout: int):
super().__init__(
message=f"Agent '{agent}' timed out",
details=f"LLM request exceeded timeout of {timeout} seconds",
suggestion=(
f"1) Try again - this might be temporary network congestion, "
f"2) Check your internet connection, "
f"3) Consider using a faster model in Settings"
),
)
class LLMContextLimitError(LLMError):
"""Raised when context length is exceeded."""
def __init__(self, agent: str, tokens: int, limit: int):
super().__init__(
message=f"Context length exceeded for agent '{agent}'",
details=f"Input requires {tokens} tokens but model limit is {limit}",
suggestion=(
f"1) Try analyzing a shorter time period, "
f"2) Use a higher timeframe (fewer data points), "
f"3) Use a model with larger context window"
),
)
# Analysis Errors
class AnalysisError(TradingAnalysisError):
"""Base class for analysis errors."""
pass
class IndicatorCalculationError(AnalysisError):
"""Raised when indicator calculation fails."""
def __init__(self, indicator: str, reason: str):
super().__init__(
message=f"Failed to calculate {indicator}",
details=reason,
suggestion=(
f"1) Ensure there's enough data for {indicator} calculation, "
f"2) Try adjusting indicator parameters in Settings, "
f"3) Check if the data quality is sufficient"
),
)
class PatternRecognitionError(AnalysisError):
"""Raised when pattern recognition fails."""
def __init__(self, reason: str):
super().__init__(
message="Pattern recognition failed",
details=reason,
suggestion="This might be due to unusual market data or chart generation issues. Try again.",
)
class ChartGenerationError(AnalysisError):
"""Raised when chart generation fails."""
def __init__(self, reason: str):
super().__init__(
message="Failed to generate chart",
details=reason,
suggestion=(
"1) Ensure matplotlib and mplfinance are installed correctly, "
"2) Check that the data directory is writable, "
"3) Verify sufficient disk space is available"
),
)
# Workflow Errors
class WorkflowError(TradingAnalysisError):
"""Base class for workflow execution errors."""
pass
class AgentExecutionError(WorkflowError):
"""Raised when an agent fails to execute."""
def __init__(self, agent: str, step: str, error: str):
super().__init__(
message=f"Agent '{agent}' failed",
details=f"Error during {step}: {error}",
suggestion=(
f"1) Check the logs for more details, "
f"2) Verify API keys are configured correctly, "
f"3) Try running the analysis again"
),
)
class WorkflowTimeoutError(WorkflowError):
"""Raised when workflow execution times out."""
def __init__(self, workflow: str, timeout: int):
super().__init__(
message=f"Workflow '{workflow}' timed out",
details=f"Analysis exceeded maximum time of {timeout} seconds",
suggestion=(
f"1) Try using Technical Analysis instead of Comprehensive for faster results, "
f"2) Check your internet connection, "
f"3) The analysis was taking too long - try again"
),
)
# Helper Functions
def format_exception_for_user(e: Exception) -> str:
"""
Format any exception into a user-friendly error message.
Args:
e: The exception to format
Returns:
Formatted error message with helpful information
"""
if isinstance(e, TradingAnalysisError):
return e.format_error()
# Handle common Python exceptions
error_type = type(e).__name__
error_msg = str(e)
# Map common exceptions to user-friendly messages
if isinstance(e, KeyError):
return (
f"β Missing required data field: {error_msg}\n"
f"π‘ Suggestion: This might be due to incomplete data from the provider. Try again."
)
elif isinstance(e, ValueError):
return (
f"β Invalid value: {error_msg}\n"
f"π‘ Suggestion: Check your input parameters and try again."
)
elif isinstance(e, ConnectionError):
return (
f"β Connection error: {error_msg}\n"
f"π‘ Suggestion: Check your internet connection and try again."
)
elif isinstance(e, TimeoutError):
return (
f"β Request timed out: {error_msg}\n"
f"π‘ Suggestion: The service is taking too long to respond. Please try again."
)
else:
return (
f"β Unexpected error ({error_type}): {error_msg}\n"
f"π‘ Suggestion: Please try again. If the problem persists, check the logs for details."
)
def wrap_provider_error(
provider: str, ticker: str, operation: str, error: Exception
) -> TradingAnalysisError:
"""
Wrap provider exceptions into appropriate TradingAnalysisError subclasses.
Args:
provider: Name of the data provider
ticker: Ticker symbol being accessed
operation: Operation being performed (e.g., "fetch_ohlc", "fetch_fundamentals")
error: The original exception
Returns:
Appropriate TradingAnalysisError subclass
"""
error_msg = str(error).lower()
# Check for specific error patterns
if "no data" in error_msg or "empty" in error_msg:
return NoDataReturnedError(ticker, "unknown", provider)
elif "rate limit" in error_msg or "too many requests" in error_msg:
return RateLimitError(provider)
elif (
"not found" in error_msg
or "invalid ticker" in error_msg
or "invalid symbol" in error_msg
):
return TickerNotFoundError(ticker, provider)
elif (
"api key" in error_msg
or "unauthorized" in error_msg
or "forbidden" in error_msg
):
return APIKeyError(provider)
else:
return DataProviderError(
message=f"{provider} error during {operation}",
details=str(error),
suggestion=(
f"1) Verify the ticker '{ticker}' is correct, "
f"2) Check your internet connection, "
f"3) Try using a different data provider in Settings"
),
)
|