-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
115 lines (82 loc) · 3.32 KB
/
exceptions.py
File metadata and controls
115 lines (82 loc) · 3.32 KB
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
"""Domain-specific exception hierarchy.
Provides structured error types for health intelligence operations
with error codes, context, and retry classification.
"""
from __future__ import annotations
from typing import Any
class BaseError(Exception):
"""Base exception for all application errors."""
def __init__(
self, message: str, code: str = "UNKNOWN", context: dict[str, Any] | None = None,
retryable: bool = False,
) -> None:
super().__init__(message)
self.code = code
self.context = context or {}
self.retryable = retryable
def to_dict(self) -> dict[str, Any]:
return {"error": self.code, "message": str(self), "context": self.context, "retryable": self.retryable}
class DataError(BaseError):
"""Errors related to data quality, format, or availability."""
pass
class ValidationError(DataError):
"""Data validation failure."""
def __init__(self, field: str, message: str, value: Any = None) -> None:
super().__init__(
f"Validation failed for {field}: {message}",
code="VALIDATION_ERROR",
context={"field": field, "value": str(value)[:200] if value is not None else None},
)
class SchemaError(DataError):
"""Schema mismatch or incompatibility."""
def __init__(self, expected: str, actual: str) -> None:
super().__init__(
f"Schema mismatch: expected {expected}, got {actual}",
code="SCHEMA_ERROR",
context={"expected": expected, "actual": actual},
)
class SourceUnavailableError(DataError):
"""External data source is unreachable."""
def __init__(self, source: str, reason: str = "") -> None:
super().__init__(
f"Source unavailable: {source}" + (f" ({reason})" if reason else ""),
code="SOURCE_UNAVAILABLE", retryable=True,
context={"source": source},
)
class PipelineError(BaseError):
"""Errors in analytical pipeline execution."""
pass
class StageError(PipelineError):
"""Failure in a specific pipeline stage."""
def __init__(self, stage: str, message: str) -> None:
super().__init__(
f"Stage '{stage}' failed: {message}",
code="STAGE_ERROR", context={"stage": stage},
)
class CheckpointError(PipelineError):
"""Checkpoint save or restore failure."""
def __init__(self, operation: str, path: str) -> None:
super().__init__(
f"Checkpoint {operation} failed: {path}",
code="CHECKPOINT_ERROR", retryable=True,
context={"operation": operation, "path": path},
)
class ModelError(BaseError):
"""Errors related to ML model operations."""
pass
class InferenceError(ModelError):
"""Model inference failure."""
def __init__(self, model_id: str, message: str) -> None:
super().__init__(
f"Inference failed for {model_id}: {message}",
code="INFERENCE_ERROR", retryable=True,
context={"model_id": model_id},
)
class TrainingError(ModelError):
"""Model training failure."""
def __init__(self, phase: str, step: int, message: str) -> None:
super().__init__(
f"Training failed at {phase} step {step}: {message}",
code="TRAINING_ERROR",
context={"phase": phase, "step": step},
)