File size: 6,993 Bytes
59ce7b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Exception Hierarchy

> **Last Updated**: 2025-12-06

This document describes all custom exceptions in DeepBoner.

## Location

All exceptions are defined in `src/utils/exceptions.py`.

## Exception Tree

```
Exception (Python builtin)
    └── DeepBonerError (base)
            β”œβ”€β”€ SearchError
            β”‚       └── RateLimitError
            β”œβ”€β”€ JudgeError
            β”œβ”€β”€ ConfigurationError
            β”œβ”€β”€ EmbeddingError
            β”œβ”€β”€ LLMError
            β”‚       └── QuotaExceededError
            └── SynthesisError
```

---

## Base Exception

### DeepBonerError

```python
class DeepBonerError(Exception):
    """Base exception for all DeepBoner errors."""
    pass
```

**When to use:** Never directly. Use specific subclasses.

**Catch when:** You want to catch all DeepBoner-related errors.

```python
try:
    result = orchestrator.run(query)
except DeepBonerError as e:
    logger.error(f"Research failed: {e}")
```

---

## Search Exceptions

### SearchError

```python
class SearchError(DeepBonerError):
    """Raised when a search operation fails."""
    pass
```

**When raised:**
- External API returns error status
- Network timeout
- Invalid response format
- No results found (in strict mode)

**Example:**
```python
from src.utils.exceptions import SearchError

if response.status_code != 200:
    raise SearchError(f"PubMed returned {response.status_code}")
```

---

### RateLimitError

```python
class RateLimitError(SearchError):
    """Raised when we hit API rate limits."""
    pass
```

**When raised:**
- HTTP 429 (Too Many Requests)
- PubMed rate limit exceeded
- ClinicalTrials.gov throttling

**Handling:**
```python
from src.utils.exceptions import RateLimitError

try:
    results = pubmed.search(query)
except RateLimitError:
    await asyncio.sleep(60)  # Wait and retry
    results = pubmed.search(query)
```

**Prevention:**
- Add `NCBI_API_KEY` for higher PubMed limits
- Use built-in rate limiter (`src/tools/rate_limiter.py`)

---

## Judge Exceptions

### JudgeError

```python
class JudgeError(DeepBonerError):
    """Raised when the judge fails to assess evidence."""
    pass
```

**When raised:**
- LLM fails to produce valid assessment
- Assessment parsing fails
- Confidence below threshold
- Invalid judge response format

**Example:**
```python
from src.utils.exceptions import JudgeError

if not assessment.details:
    raise JudgeError("Judge produced incomplete assessment")
```

---

## Configuration Exceptions

### ConfigurationError

```python
class ConfigurationError(DeepBonerError):
    """Raised when configuration is invalid."""
    pass
```

**When raised:**
- Required API key missing
- Invalid setting value
- Environment variable malformed
- Conflicting configuration

**Example:**
```python
from src.utils.exceptions import ConfigurationError

def get_api_key(self) -> str:
    if not self.openai_api_key:
        raise ConfigurationError("OPENAI_API_KEY not set")
    return self.openai_api_key
```

---

## Embedding Exceptions

### EmbeddingError

```python
class EmbeddingError(DeepBonerError):
    """Raised when embedding or vector store operations fail."""
    pass
```

**When raised:**
- ChromaDB connection failure
- Sentence-transformers model load failure
- Vector dimension mismatch
- Embedding generation fails

**Example:**
```python
from src.utils.exceptions import EmbeddingError

try:
    embeddings = model.encode(texts)
except Exception as e:
    raise EmbeddingError(f"Embedding failed: {e}")
```

---

## LLM Exceptions

### LLMError

```python
class LLMError(DeepBonerError):
    """Raised when LLM operations fail (API errors, parsing errors, etc.)."""
    pass
```

**When raised:**
- LLM API error
- Response parsing failure
- Invalid model output
- Context length exceeded

---

### QuotaExceededError

```python
class QuotaExceededError(LLMError):
    """Raised when LLM API quota is exceeded (402 errors)."""
    pass
```

**When raised:**
- OpenAI billing limit hit
- HuggingFace rate limit exceeded
- HTTP 402 Payment Required

**Handling:**
```python
from src.utils.exceptions import QuotaExceededError

try:
    response = client.chat_completion(messages)
except QuotaExceededError:
    # Fall back to free tier or notify user
    return fallback_response()
```

---

## Synthesis Exceptions

### SynthesisError

```python
class SynthesisError(DeepBonerError):
    """Raised when report synthesis fails after trying all available models.

    Attributes:
        message: Human-readable error description
        attempted_models: List of model IDs that were tried
        errors: List of error messages from each failed attempt
    """

    def __init__(
        self,
        message: str,
        attempted_models: list[str] | None = None,
        errors: list[str] | None = None,
    ) -> None:
        super().__init__(message)
        self.attempted_models = attempted_models or []
        self.errors = errors or []
```

**When raised:**
- All LLM models fail to synthesize report
- Report generation exceeds retry limit

**Example:**
```python
from src.utils.exceptions import SynthesisError

if all_attempts_failed:
    raise SynthesisError(
        "Failed to synthesize report",
        attempted_models=["gpt-5", "gpt-4o"],
        errors=["Rate limit", "Context too long"]
    )
```

**Accessing details:**
```python
try:
    report = synthesize(evidence)
except SynthesisError as e:
    print(f"Failed: {e}")
    print(f"Tried models: {e.attempted_models}")
    print(f"Errors: {e.errors}")
```

---

## Usage Patterns

### Catching Specific Exceptions

```python
from src.utils.exceptions import (
    SearchError,
    RateLimitError,
    JudgeError,
)

try:
    result = orchestrator.run(query)
except RateLimitError:
    # Specific handling for rate limits
    await rate_limiter.wait()
    result = orchestrator.run(query)
except SearchError:
    # General search failure
    return empty_result()
except JudgeError:
    # Judge failed, use default assessment
    return default_assessment()
```

### Exception Chaining

```python
try:
    response = api_call()
except requests.RequestException as e:
    raise SearchError(f"API call failed: {e}") from e
```

### Logging Exceptions

```python
import structlog

logger = structlog.get_logger()

try:
    results = search(query)
except DeepBonerError as e:
    logger.error("operation_failed", error=str(e), exc_info=True)
    raise
```

---

## Best Practices

1. **Use specific exceptions** - Don't raise `DeepBonerError` directly
2. **Include context** - Error messages should explain what failed
3. **Chain exceptions** - Use `from e` to preserve stack trace
4. **Log before re-raising** - Capture context for debugging
5. **Handle at boundaries** - Catch exceptions at API/UI boundaries

---

## Related Documentation

- [Component Inventory](component-inventory.md)
- [Data Models](data-models.md)
- [Troubleshooting](../getting-started/troubleshooting.md)