cfsa-antigravity 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/package.json +1 -1
- package/template/.agent/instructions/commands.md +8 -32
- package/template/.agent/instructions/example.md +21 -0
- package/template/.agent/instructions/patterns.md +3 -3
- package/template/.agent/instructions/tech-stack.md +71 -23
- package/template/.agent/instructions/workflow.md +12 -1
- package/template/.agent/rules/completion-checklist.md +6 -0
- package/template/.agent/rules/security-first.md +3 -3
- package/template/.agent/rules/vertical-slices.md +1 -1
- package/template/.agent/skill-library/MANIFEST.md +6 -0
- package/template/.agent/skill-library/stack/devops/git-advanced/SKILL.md +972 -0
- package/template/.agent/skill-library/stack/devops/git-workflow/SKILL.md +420 -0
- package/template/.agent/skills/api-versioning/SKILL.md +44 -298
- package/template/.agent/skills/api-versioning/references/typescript.md +157 -0
- package/template/.agent/skills/architecture-mapping/SKILL.md +13 -13
- package/template/.agent/skills/bootstrap-agents/SKILL.md +151 -152
- package/template/.agent/skills/clean-code/SKILL.md +64 -118
- package/template/.agent/skills/clean-code/references/typescript.md +126 -0
- package/template/.agent/skills/database-schema-design/SKILL.md +93 -317
- package/template/.agent/skills/database-schema-design/references/relational.md +228 -0
- package/template/.agent/skills/error-handling-patterns/SKILL.md +62 -557
- package/template/.agent/skills/error-handling-patterns/references/go.md +162 -0
- package/template/.agent/skills/error-handling-patterns/references/python.md +262 -0
- package/template/.agent/skills/error-handling-patterns/references/rust.md +112 -0
- package/template/.agent/skills/error-handling-patterns/references/typescript.md +178 -0
- package/template/.agent/skills/idea-extraction/SKILL.md +322 -224
- package/template/.agent/skills/logging-best-practices/SKILL.md +108 -767
- package/template/.agent/skills/logging-best-practices/references/go.md +49 -0
- package/template/.agent/skills/logging-best-practices/references/python.md +52 -0
- package/template/.agent/skills/logging-best-practices/references/typescript.md +215 -0
- package/template/.agent/skills/migration-management/SKILL.md +127 -311
- package/template/.agent/skills/migration-management/references/relational.md +214 -0
- package/template/.agent/skills/parallel-feature-development/SKILL.md +34 -43
- package/template/.agent/skills/pipeline-rubrics/references/be-rubric.md +1 -1
- package/template/.agent/skills/pipeline-rubrics/references/ia-rubric.md +2 -2
- package/template/.agent/skills/pipeline-rubrics/references/scoring.md +1 -1
- package/template/.agent/skills/pipeline-rubrics/references/vision-rubric.md +2 -1
- package/template/.agent/skills/prd-templates/SKILL.md +23 -6
- package/template/.agent/skills/prd-templates/references/be-spec-template.md +2 -2
- package/template/.agent/skills/prd-templates/references/decomposition-templates.md +2 -2
- package/template/.agent/skills/prd-templates/references/engineering-standards-template.md +2 -0
- package/template/.agent/skills/prd-templates/references/fe-spec-template.md +1 -1
- package/template/.agent/skills/prd-templates/references/fractal-cx-template.md +58 -0
- package/template/.agent/skills/prd-templates/references/fractal-feature-template.md +93 -0
- package/template/.agent/skills/prd-templates/references/fractal-node-index-template.md +55 -0
- package/template/.agent/skills/prd-templates/references/ideation-crosscut-template.md +26 -47
- package/template/.agent/skills/prd-templates/references/ideation-index-template.md +47 -31
- package/template/.agent/skills/prd-templates/references/operational-templates.md +1 -1
- package/template/.agent/skills/prd-templates/references/placeholder-workflow-mapping.md +50 -21
- package/template/.agent/skills/prd-templates/references/skill-loading-protocol.md +32 -0
- package/template/.agent/skills/prd-templates/references/slice-completion-gates.md +29 -0
- package/template/.agent/skills/prd-templates/references/spec-coverage-sweep.md +3 -3
- package/template/.agent/skills/prd-templates/references/tdd-testing-policy.md +39 -0
- package/template/.agent/skills/prd-templates/references/vision-template.md +8 -8
- package/template/.agent/skills/regex-patterns/SKILL.md +122 -540
- package/template/.agent/skills/regex-patterns/references/go.md +44 -0
- package/template/.agent/skills/regex-patterns/references/javascript.md +63 -0
- package/template/.agent/skills/regex-patterns/references/python.md +77 -0
- package/template/.agent/skills/regex-patterns/references/rust.md +43 -0
- package/template/.agent/skills/resolve-ambiguity/SKILL.md +1 -1
- package/template/.agent/skills/session-continuity/SKILL.md +11 -9
- package/template/.agent/skills/session-continuity/protocols/02-progress-generation.md +2 -2
- package/template/.agent/skills/session-continuity/protocols/04-pattern-extraction.md +1 -1
- package/template/.agent/skills/session-continuity/protocols/05-session-close.md +1 -1
- package/template/.agent/skills/session-continuity/protocols/09-parallel-claim.md +1 -1
- package/template/.agent/skills/session-continuity/protocols/10-placeholder-verification-gate.md +57 -78
- package/template/.agent/skills/session-continuity/protocols/11-parallel-synthesis.md +1 -1
- package/template/.agent/skills/spec-writing/SKILL.md +1 -1
- package/template/.agent/skills/tdd-workflow/SKILL.md +94 -317
- package/template/.agent/skills/tdd-workflow/references/typescript.md +231 -0
- package/template/.agent/skills/testing-strategist/SKILL.md +74 -687
- package/template/.agent/skills/testing-strategist/references/typescript.md +328 -0
- package/template/.agent/skills/workflow-automation/SKILL.md +62 -154
- package/template/.agent/skills/workflow-automation/references/inngest.md +88 -0
- package/template/.agent/skills/workflow-automation/references/temporal.md +64 -0
- package/template/.agent/workflows/bootstrap-agents-fill.md +85 -143
- package/template/.agent/workflows/bootstrap-agents-provision.md +90 -107
- package/template/.agent/workflows/create-prd-architecture.md +23 -16
- package/template/.agent/workflows/create-prd-compile.md +11 -12
- package/template/.agent/workflows/create-prd-design-system.md +1 -1
- package/template/.agent/workflows/create-prd-security.md +9 -11
- package/template/.agent/workflows/create-prd-stack.md +10 -4
- package/template/.agent/workflows/create-prd.md +9 -9
- package/template/.agent/workflows/decompose-architecture-structure.md +4 -6
- package/template/.agent/workflows/decompose-architecture-validate.md +18 -1
- package/template/.agent/workflows/decompose-architecture.md +18 -3
- package/template/.agent/workflows/evolve-contract.md +11 -11
- package/template/.agent/workflows/evolve-feature-classify.md +14 -6
- package/template/.agent/workflows/ideate-discover.md +72 -107
- package/template/.agent/workflows/ideate-extract.md +84 -63
- package/template/.agent/workflows/ideate-validate.md +26 -22
- package/template/.agent/workflows/ideate.md +9 -9
- package/template/.agent/workflows/implement-slice-setup.md +25 -23
- package/template/.agent/workflows/implement-slice-tdd.md +73 -89
- package/template/.agent/workflows/implement-slice.md +4 -4
- package/template/.agent/workflows/plan-phase-preflight.md +6 -2
- package/template/.agent/workflows/plan-phase-write.md +6 -8
- package/template/.agent/workflows/remediate-pipeline-assess.md +2 -1
- package/template/.agent/workflows/resolve-ambiguity.md +2 -2
- package/template/.agent/workflows/update-architecture-map.md +22 -5
- package/template/.agent/workflows/validate-phase-quality.md +155 -0
- package/template/.agent/workflows/validate-phase-readiness.md +167 -0
- package/template/.agent/workflows/validate-phase.md +19 -157
- package/template/.agent/workflows/verify-infrastructure.md +10 -10
- package/template/.agent/workflows/write-architecture-spec-design.md +23 -14
- package/template/.agent/workflows/write-be-spec-classify.md +25 -21
- package/template/.agent/workflows/write-be-spec.md +1 -1
- package/template/.agent/workflows/write-fe-spec-classify.md +6 -12
- package/template/.agent/workflows/write-fe-spec-write.md +1 -1
- package/template/AGENTS.md +6 -2
- package/template/GEMINI.md +5 -3
- package/template/docs/README.md +10 -10
- package/template/docs/kit-architecture.md +126 -33
- package/template/docs/plans/ideation/README.md +8 -3
- package/template/.agent/skills/prd-templates/references/ideation-domain-template.md +0 -55
|
@@ -18,16 +18,30 @@ Build resilient applications with robust error handling strategies that graceful
|
|
|
18
18
|
- Handling async/concurrent errors
|
|
19
19
|
- Building fault-tolerant distributed systems
|
|
20
20
|
|
|
21
|
+
## Stack-Specific References
|
|
22
|
+
|
|
23
|
+
After reading the methodology below, read the reference matching your surface's Languages column in the surface stack map (`.agent/instructions/tech-stack.md`):
|
|
24
|
+
|
|
25
|
+
| Language | Reference |
|
|
26
|
+
|----------|-----------|
|
|
27
|
+
| TypeScript / JavaScript | `references/typescript.md` |
|
|
28
|
+
| Python | `references/python.md` |
|
|
29
|
+
| Go | `references/go.md` |
|
|
30
|
+
| Rust | `references/rust.md` |
|
|
31
|
+
|
|
21
32
|
## Core Concepts
|
|
22
33
|
|
|
23
34
|
### 1. Error Handling Philosophies
|
|
24
35
|
|
|
25
|
-
|
|
36
|
+
Every language takes a different approach to error handling. Know which philosophy your language uses and follow its idioms:
|
|
26
37
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
| Philosophy | Description | Languages |
|
|
39
|
+
|-----------|-------------|-----------|
|
|
40
|
+
| **Exceptions** | Traditional try-catch, disrupts control flow | Python, TypeScript/JS, Java, C# |
|
|
41
|
+
| **Result Types** | Explicit success/failure, functional approach | Rust, Haskell, modern TypeScript |
|
|
42
|
+
| **Error Returns** | Explicit error as return value, requires discipline | Go |
|
|
43
|
+
| **Option/Maybe Types** | For nullable values without exceptions | Rust, Haskell, Swift |
|
|
44
|
+
| **Panics/Crashes** | Unrecoverable errors, programming bugs | Rust (panic), Go (panic), Python (SystemExit) |
|
|
31
45
|
|
|
32
46
|
**When to Use Each:**
|
|
33
47
|
|
|
@@ -37,533 +51,77 @@ Build resilient applications with robust error handling strategies that graceful
|
|
|
37
51
|
|
|
38
52
|
### 2. Error Categories
|
|
39
53
|
|
|
40
|
-
**Recoverable Errors
|
|
54
|
+
**Recoverable Errors** — the application can handle and continue:
|
|
41
55
|
|
|
42
56
|
- Network timeouts
|
|
43
57
|
- Missing files
|
|
44
58
|
- Invalid user input
|
|
45
59
|
- API rate limits
|
|
46
60
|
|
|
47
|
-
**Unrecoverable Errors
|
|
61
|
+
**Unrecoverable Errors** — the application should crash and restart:
|
|
48
62
|
|
|
49
63
|
- Out of memory
|
|
50
64
|
- Stack overflow
|
|
51
65
|
- Programming bugs (null pointer, etc.)
|
|
52
66
|
|
|
53
|
-
##
|
|
54
|
-
|
|
55
|
-
### Python Error Handling
|
|
56
|
-
|
|
57
|
-
**Custom Exception Hierarchy:**
|
|
58
|
-
|
|
59
|
-
```python
|
|
60
|
-
class ApplicationError(Exception):
|
|
61
|
-
"""Base exception for all application errors."""
|
|
62
|
-
def __init__(self, message: str, code: str = None, details: dict = None):
|
|
63
|
-
super().__init__(message)
|
|
64
|
-
self.code = code
|
|
65
|
-
self.details = details or {}
|
|
66
|
-
self.timestamp = datetime.utcnow()
|
|
67
|
-
|
|
68
|
-
class ValidationError(ApplicationError):
|
|
69
|
-
"""Raised when validation fails."""
|
|
70
|
-
pass
|
|
71
|
-
|
|
72
|
-
class NotFoundError(ApplicationError):
|
|
73
|
-
"""Raised when resource not found."""
|
|
74
|
-
pass
|
|
75
|
-
|
|
76
|
-
class ExternalServiceError(ApplicationError):
|
|
77
|
-
"""Raised when external service fails."""
|
|
78
|
-
def __init__(self, message: str, service: str, **kwargs):
|
|
79
|
-
super().__init__(message, **kwargs)
|
|
80
|
-
self.service = service
|
|
81
|
-
|
|
82
|
-
# Usage
|
|
83
|
-
def get_user(user_id: str) -> User:
|
|
84
|
-
user = db.query(User).filter_by(id=user_id).first()
|
|
85
|
-
if not user:
|
|
86
|
-
raise NotFoundError(
|
|
87
|
-
f"User not found",
|
|
88
|
-
code="USER_NOT_FOUND",
|
|
89
|
-
details={"user_id": user_id}
|
|
90
|
-
)
|
|
91
|
-
return user
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
**Context Managers for Cleanup:**
|
|
95
|
-
|
|
96
|
-
```python
|
|
97
|
-
from contextlib import contextmanager
|
|
98
|
-
|
|
99
|
-
@contextmanager
|
|
100
|
-
def database_transaction(session):
|
|
101
|
-
"""Ensure transaction is committed or rolled back."""
|
|
102
|
-
try:
|
|
103
|
-
yield session
|
|
104
|
-
session.commit()
|
|
105
|
-
except Exception as e:
|
|
106
|
-
session.rollback()
|
|
107
|
-
raise
|
|
108
|
-
finally:
|
|
109
|
-
session.close()
|
|
110
|
-
|
|
111
|
-
# Usage
|
|
112
|
-
with database_transaction(db.session) as session:
|
|
113
|
-
user = User(name="Alice")
|
|
114
|
-
session.add(user)
|
|
115
|
-
# Automatic commit or rollback
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
**Retry with Exponential Backoff:**
|
|
119
|
-
|
|
120
|
-
```python
|
|
121
|
-
import time
|
|
122
|
-
from functools import wraps
|
|
123
|
-
from typing import TypeVar, Callable
|
|
124
|
-
|
|
125
|
-
T = TypeVar('T')
|
|
126
|
-
|
|
127
|
-
def retry(
|
|
128
|
-
max_attempts: int = 3,
|
|
129
|
-
backoff_factor: float = 2.0,
|
|
130
|
-
exceptions: tuple = (Exception,)
|
|
131
|
-
):
|
|
132
|
-
"""Retry decorator with exponential backoff."""
|
|
133
|
-
def decorator(func: Callable[..., T]) -> Callable[..., T]:
|
|
134
|
-
@wraps(func)
|
|
135
|
-
def wrapper(*args, **kwargs) -> T:
|
|
136
|
-
last_exception = None
|
|
137
|
-
for attempt in range(max_attempts):
|
|
138
|
-
try:
|
|
139
|
-
return func(*args, **kwargs)
|
|
140
|
-
except exceptions as e:
|
|
141
|
-
last_exception = e
|
|
142
|
-
if attempt < max_attempts - 1:
|
|
143
|
-
sleep_time = backoff_factor ** attempt
|
|
144
|
-
time.sleep(sleep_time)
|
|
145
|
-
continue
|
|
146
|
-
raise
|
|
147
|
-
raise last_exception
|
|
148
|
-
return wrapper
|
|
149
|
-
return decorator
|
|
150
|
-
|
|
151
|
-
# Usage
|
|
152
|
-
@retry(max_attempts=3, exceptions=(NetworkError,))
|
|
153
|
-
def fetch_data(url: str) -> dict:
|
|
154
|
-
response = requests.get(url, timeout=5)
|
|
155
|
-
response.raise_for_status()
|
|
156
|
-
return response.json()
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### TypeScript/JavaScript Error Handling
|
|
160
|
-
|
|
161
|
-
**Custom Error Classes:**
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
// Custom error classes
|
|
165
|
-
class ApplicationError extends Error {
|
|
166
|
-
constructor(
|
|
167
|
-
message: string,
|
|
168
|
-
public code: string,
|
|
169
|
-
public statusCode: number = 500,
|
|
170
|
-
public details?: Record<string, any>,
|
|
171
|
-
) {
|
|
172
|
-
super(message);
|
|
173
|
-
this.name = this.constructor.name;
|
|
174
|
-
Error.captureStackTrace(this, this.constructor);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
class ValidationError extends ApplicationError {
|
|
179
|
-
constructor(message: string, details?: Record<string, any>) {
|
|
180
|
-
super(message, "VALIDATION_ERROR", 400, details);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
class NotFoundError extends ApplicationError {
|
|
185
|
-
constructor(resource: string, id: string) {
|
|
186
|
-
super(`${resource} not found`, "NOT_FOUND", 404, { resource, id });
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// Usage
|
|
191
|
-
function getUser(id: string): User {
|
|
192
|
-
const user = users.find((u) => u.id === id);
|
|
193
|
-
if (!user) {
|
|
194
|
-
throw new NotFoundError("User", id);
|
|
195
|
-
}
|
|
196
|
-
return user;
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
**Result Type Pattern:**
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
// Result type for explicit error handling
|
|
204
|
-
type Result<T, E = Error> = { ok: true; value: T } | { ok: false; error: E };
|
|
67
|
+
## Universal Patterns
|
|
205
68
|
|
|
206
|
-
|
|
207
|
-
function Ok<T>(value: T): Result<T, never> {
|
|
208
|
-
return { ok: true, value };
|
|
209
|
-
}
|
|
69
|
+
### Pattern 1: Circuit Breaker
|
|
210
70
|
|
|
211
|
-
|
|
212
|
-
return { ok: false, error };
|
|
213
|
-
}
|
|
71
|
+
Prevent cascading failures in distributed systems. When a downstream service fails repeatedly, stop calling it temporarily to let it recover.
|
|
214
72
|
|
|
215
|
-
|
|
216
|
-
function parseJSON<T>(json: string): Result<T, SyntaxError> {
|
|
217
|
-
try {
|
|
218
|
-
const value = JSON.parse(json) as T;
|
|
219
|
-
return Ok(value);
|
|
220
|
-
} catch (error) {
|
|
221
|
-
return Err(error as SyntaxError);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
73
|
+
**State machine:**
|
|
224
74
|
|
|
225
|
-
// Consuming Result
|
|
226
|
-
const result = parseJSON<User>(userJson);
|
|
227
|
-
if (result.ok) {
|
|
228
|
-
console.log(result.value.name);
|
|
229
|
-
} else {
|
|
230
|
-
console.error("Parse failed:", result.error.message);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Chaining Results
|
|
234
|
-
function chain<T, U, E>(
|
|
235
|
-
result: Result<T, E>,
|
|
236
|
-
fn: (value: T) => Result<U, E>,
|
|
237
|
-
): Result<U, E> {
|
|
238
|
-
return result.ok ? fn(result.value) : result;
|
|
239
|
-
}
|
|
240
75
|
```
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
// Async/await with proper error handling
|
|
246
|
-
async function fetchUserOrders(userId: string): Promise<Order[]> {
|
|
247
|
-
try {
|
|
248
|
-
const user = await getUser(userId);
|
|
249
|
-
const orders = await getOrders(user.id);
|
|
250
|
-
return orders;
|
|
251
|
-
} catch (error) {
|
|
252
|
-
if (error instanceof NotFoundError) {
|
|
253
|
-
return []; // Return empty array for not found
|
|
254
|
-
}
|
|
255
|
-
if (error instanceof NetworkError) {
|
|
256
|
-
// Retry logic
|
|
257
|
-
return retryFetchOrders(userId);
|
|
258
|
-
}
|
|
259
|
-
// Re-throw unexpected errors
|
|
260
|
-
throw error;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Promise error handling
|
|
265
|
-
function fetchData(url: string): Promise<Data> {
|
|
266
|
-
return fetch(url)
|
|
267
|
-
.then((response) => {
|
|
268
|
-
if (!response.ok) {
|
|
269
|
-
throw new NetworkError(`HTTP ${response.status}`);
|
|
270
|
-
}
|
|
271
|
-
return response.json();
|
|
272
|
-
})
|
|
273
|
-
.catch((error) => {
|
|
274
|
-
console.error("Fetch failed:", error);
|
|
275
|
-
throw error;
|
|
276
|
-
});
|
|
277
|
-
}
|
|
76
|
+
CLOSED → (failures ≥ threshold) → OPEN
|
|
77
|
+
OPEN → (timeout elapsed) → HALF_OPEN
|
|
78
|
+
HALF_OPEN → (success ≥ threshold) → CLOSED
|
|
79
|
+
HALF_OPEN → (any failure) → OPEN
|
|
278
80
|
```
|
|
279
81
|
|
|
280
|
-
|
|
82
|
+
**Parameters:**
|
|
83
|
+
- `failure_threshold` — how many failures before opening the circuit (e.g., 5)
|
|
84
|
+
- `timeout` — how long to stay open before trying again (e.g., 60s)
|
|
85
|
+
- `success_threshold` — how many successes in HALF_OPEN before closing (e.g., 2)
|
|
281
86
|
|
|
282
|
-
|
|
87
|
+
See language-specific references for implementation examples.
|
|
283
88
|
|
|
284
|
-
|
|
285
|
-
use std::fs::File;
|
|
286
|
-
use std::io::{self, Read};
|
|
287
|
-
|
|
288
|
-
// Result type for operations that can fail
|
|
289
|
-
fn read_file(path: &str) -> Result<String, io::Error> {
|
|
290
|
-
let mut file = File::open(path)?; // ? operator propagates errors
|
|
291
|
-
let mut contents = String::new();
|
|
292
|
-
file.read_to_string(&mut contents)?;
|
|
293
|
-
Ok(contents)
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Custom error types
|
|
297
|
-
#[derive(Debug)]
|
|
298
|
-
enum AppError {
|
|
299
|
-
Io(io::Error),
|
|
300
|
-
Parse(std::num::ParseIntError),
|
|
301
|
-
NotFound(String),
|
|
302
|
-
Validation(String),
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
impl From<io::Error> for AppError {
|
|
306
|
-
fn from(error: io::Error) -> Self {
|
|
307
|
-
AppError::Io(error)
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// Using custom error type
|
|
312
|
-
fn read_number_from_file(path: &str) -> Result<i32, AppError> {
|
|
313
|
-
let contents = read_file(path)?; // Auto-converts io::Error
|
|
314
|
-
let number = contents.trim().parse()
|
|
315
|
-
.map_err(AppError::Parse)?; // Explicitly convert ParseIntError
|
|
316
|
-
Ok(number)
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// Option for nullable values
|
|
320
|
-
fn find_user(id: &str) -> Option<User> {
|
|
321
|
-
users.iter().find(|u| u.id == id).cloned()
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// Combining Option and Result
|
|
325
|
-
fn get_user_age(id: &str) -> Result<u32, AppError> {
|
|
326
|
-
find_user(id)
|
|
327
|
-
.ok_or_else(|| AppError::NotFound(id.to_string()))
|
|
328
|
-
.map(|user| user.age)
|
|
329
|
-
}
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
### Go Error Handling
|
|
333
|
-
|
|
334
|
-
**Explicit Error Returns:**
|
|
335
|
-
|
|
336
|
-
```go
|
|
337
|
-
// Basic error handling
|
|
338
|
-
func getUser(id string) (*User, error) {
|
|
339
|
-
user, err := db.QueryUser(id)
|
|
340
|
-
if err != nil {
|
|
341
|
-
return nil, fmt.Errorf("failed to query user: %w", err)
|
|
342
|
-
}
|
|
343
|
-
if user == nil {
|
|
344
|
-
return nil, errors.New("user not found")
|
|
345
|
-
}
|
|
346
|
-
return user, nil
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// Custom error types
|
|
350
|
-
type ValidationError struct {
|
|
351
|
-
Field string
|
|
352
|
-
Message string
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
func (e *ValidationError) Error() string {
|
|
356
|
-
return fmt.Sprintf("validation failed for %s: %s", e.Field, e.Message)
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// Sentinel errors for comparison
|
|
360
|
-
var (
|
|
361
|
-
ErrNotFound = errors.New("not found")
|
|
362
|
-
ErrUnauthorized = errors.New("unauthorized")
|
|
363
|
-
ErrInvalidInput = errors.New("invalid input")
|
|
364
|
-
)
|
|
365
|
-
|
|
366
|
-
// Error checking
|
|
367
|
-
user, err := getUser("123")
|
|
368
|
-
if err != nil {
|
|
369
|
-
if errors.Is(err, ErrNotFound) {
|
|
370
|
-
// Handle not found
|
|
371
|
-
} else {
|
|
372
|
-
// Handle other errors
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
// Error wrapping and unwrapping
|
|
377
|
-
func processUser(id string) error {
|
|
378
|
-
user, err := getUser(id)
|
|
379
|
-
if err != nil {
|
|
380
|
-
return fmt.Errorf("process user failed: %w", err)
|
|
381
|
-
}
|
|
382
|
-
// Process user
|
|
383
|
-
return nil
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// Unwrap errors
|
|
387
|
-
err := processUser("123")
|
|
388
|
-
if err != nil {
|
|
389
|
-
var valErr *ValidationError
|
|
390
|
-
if errors.As(err, &valErr) {
|
|
391
|
-
fmt.Printf("Validation error: %s\n", valErr.Field)
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
```
|
|
89
|
+
### Pattern 2: Error Aggregation
|
|
395
90
|
|
|
396
|
-
|
|
91
|
+
Collect multiple errors instead of failing on first error. Essential for form validation, batch processing, and multi-field input.
|
|
397
92
|
|
|
398
|
-
|
|
93
|
+
**The pattern:**
|
|
94
|
+
1. Create an error collector
|
|
95
|
+
2. Validate each field/item independently, adding errors to the collector
|
|
96
|
+
3. After all validations, check if errors exist
|
|
97
|
+
4. If yes, throw/return all errors at once
|
|
399
98
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
```python
|
|
403
|
-
from enum import Enum
|
|
404
|
-
from datetime import datetime, timedelta
|
|
405
|
-
from typing import Callable, TypeVar
|
|
406
|
-
|
|
407
|
-
T = TypeVar('T')
|
|
408
|
-
|
|
409
|
-
class CircuitState(Enum):
|
|
410
|
-
CLOSED = "closed" # Normal operation
|
|
411
|
-
OPEN = "open" # Failing, reject requests
|
|
412
|
-
HALF_OPEN = "half_open" # Testing if recovered
|
|
413
|
-
|
|
414
|
-
class CircuitBreaker:
|
|
415
|
-
def __init__(
|
|
416
|
-
self,
|
|
417
|
-
failure_threshold: int = 5,
|
|
418
|
-
timeout: timedelta = timedelta(seconds=60),
|
|
419
|
-
success_threshold: int = 2
|
|
420
|
-
):
|
|
421
|
-
self.failure_threshold = failure_threshold
|
|
422
|
-
self.timeout = timeout
|
|
423
|
-
self.success_threshold = success_threshold
|
|
424
|
-
self.failure_count = 0
|
|
425
|
-
self.success_count = 0
|
|
426
|
-
self.state = CircuitState.CLOSED
|
|
427
|
-
self.last_failure_time = None
|
|
428
|
-
|
|
429
|
-
def call(self, func: Callable[[], T]) -> T:
|
|
430
|
-
if self.state == CircuitState.OPEN:
|
|
431
|
-
if datetime.now() - self.last_failure_time > self.timeout:
|
|
432
|
-
self.state = CircuitState.HALF_OPEN
|
|
433
|
-
self.success_count = 0
|
|
434
|
-
else:
|
|
435
|
-
raise Exception("Circuit breaker is OPEN")
|
|
436
|
-
|
|
437
|
-
try:
|
|
438
|
-
result = func()
|
|
439
|
-
self.on_success()
|
|
440
|
-
return result
|
|
441
|
-
except Exception as e:
|
|
442
|
-
self.on_failure()
|
|
443
|
-
raise
|
|
444
|
-
|
|
445
|
-
def on_success(self):
|
|
446
|
-
self.failure_count = 0
|
|
447
|
-
if self.state == CircuitState.HALF_OPEN:
|
|
448
|
-
self.success_count += 1
|
|
449
|
-
if self.success_count >= self.success_threshold:
|
|
450
|
-
self.state = CircuitState.CLOSED
|
|
451
|
-
self.success_count = 0
|
|
452
|
-
|
|
453
|
-
def on_failure(self):
|
|
454
|
-
self.failure_count += 1
|
|
455
|
-
self.last_failure_time = datetime.now()
|
|
456
|
-
if self.failure_count >= self.failure_threshold:
|
|
457
|
-
self.state = CircuitState.OPEN
|
|
458
|
-
|
|
459
|
-
# Usage
|
|
460
|
-
circuit_breaker = CircuitBreaker()
|
|
461
|
-
|
|
462
|
-
def fetch_data():
|
|
463
|
-
return circuit_breaker.call(lambda: external_api.get_data())
|
|
464
|
-
```
|
|
465
|
-
|
|
466
|
-
### Pattern 2: Error Aggregation
|
|
99
|
+
See language-specific references for implementation examples.
|
|
467
100
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
```typescript
|
|
471
|
-
class ErrorCollector {
|
|
472
|
-
private errors: Error[] = [];
|
|
473
|
-
|
|
474
|
-
add(error: Error): void {
|
|
475
|
-
this.errors.push(error);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
hasErrors(): boolean {
|
|
479
|
-
return this.errors.length > 0;
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
getErrors(): Error[] {
|
|
483
|
-
return [...this.errors];
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
throw(): never {
|
|
487
|
-
if (this.errors.length === 1) {
|
|
488
|
-
throw this.errors[0];
|
|
489
|
-
}
|
|
490
|
-
throw new AggregateError(
|
|
491
|
-
this.errors,
|
|
492
|
-
`${this.errors.length} errors occurred`,
|
|
493
|
-
);
|
|
494
|
-
}
|
|
495
|
-
}
|
|
101
|
+
### Pattern 3: Graceful Degradation
|
|
496
102
|
|
|
497
|
-
|
|
498
|
-
function validateUser(data: any): User {
|
|
499
|
-
const errors = new ErrorCollector();
|
|
103
|
+
Provide fallback functionality when errors occur. The system continues operating with reduced capability rather than failing completely.
|
|
500
104
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
105
|
+
**Strategies:**
|
|
106
|
+
- **Primary/fallback chain** — try cache, fall back to database, fall back to default
|
|
107
|
+
- **Multiple provider fallback** — try provider A, then B, then C
|
|
108
|
+
- **Feature degradation** — disable non-essential features when dependencies fail
|
|
109
|
+
- **Default values** — return safe defaults when data is temporarily unavailable
|
|
506
110
|
|
|
507
|
-
|
|
508
|
-
errors.add(new ValidationError("Name must be at least 2 characters"));
|
|
509
|
-
}
|
|
111
|
+
See language-specific references for implementation examples.
|
|
510
112
|
|
|
511
|
-
|
|
512
|
-
errors.add(new ValidationError("Age must be 18 or older"));
|
|
513
|
-
}
|
|
113
|
+
### Pattern 4: Retry with Exponential Backoff
|
|
514
114
|
|
|
515
|
-
|
|
516
|
-
errors.throw();
|
|
517
|
-
}
|
|
115
|
+
Automatically retry failed operations with increasing delays between attempts.
|
|
518
116
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
117
|
+
**Parameters:**
|
|
118
|
+
- `max_attempts` — maximum retry count (e.g., 3)
|
|
119
|
+
- `backoff_factor` — multiplier for delay between attempts (e.g., 2.0)
|
|
120
|
+
- `retryable_exceptions` — which errors are worth retrying (network, timeout — NOT validation)
|
|
522
121
|
|
|
523
|
-
|
|
122
|
+
**Delay formula:** `delay = backoff_factor ^ attempt` (1s, 2s, 4s, 8s, ...)
|
|
524
123
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
```python
|
|
528
|
-
from typing import Optional, Callable, TypeVar
|
|
529
|
-
|
|
530
|
-
T = TypeVar('T')
|
|
531
|
-
|
|
532
|
-
def with_fallback(
|
|
533
|
-
primary: Callable[[], T],
|
|
534
|
-
fallback: Callable[[], T],
|
|
535
|
-
log_error: bool = True
|
|
536
|
-
) -> T:
|
|
537
|
-
"""Try primary function, fall back to fallback on error."""
|
|
538
|
-
try:
|
|
539
|
-
return primary()
|
|
540
|
-
except Exception as e:
|
|
541
|
-
if log_error:
|
|
542
|
-
logger.error(f"Primary function failed: {e}")
|
|
543
|
-
return fallback()
|
|
544
|
-
|
|
545
|
-
# Usage
|
|
546
|
-
def get_user_profile(user_id: str) -> UserProfile:
|
|
547
|
-
return with_fallback(
|
|
548
|
-
primary=lambda: fetch_from_cache(user_id),
|
|
549
|
-
fallback=lambda: fetch_from_database(user_id)
|
|
550
|
-
)
|
|
551
|
-
|
|
552
|
-
# Multiple fallbacks
|
|
553
|
-
def get_exchange_rate(currency: str) -> float:
|
|
554
|
-
return (
|
|
555
|
-
try_function(lambda: api_provider_1.get_rate(currency))
|
|
556
|
-
or try_function(lambda: api_provider_2.get_rate(currency))
|
|
557
|
-
or try_function(lambda: cache.get_rate(currency))
|
|
558
|
-
or DEFAULT_RATE
|
|
559
|
-
)
|
|
560
|
-
|
|
561
|
-
def try_function(func: Callable[[], Optional[T]]) -> Optional[T]:
|
|
562
|
-
try:
|
|
563
|
-
return func()
|
|
564
|
-
except Exception:
|
|
565
|
-
return None
|
|
566
|
-
```
|
|
124
|
+
See language-specific references for implementation examples.
|
|
567
125
|
|
|
568
126
|
## Best Practices
|
|
569
127
|
|
|
@@ -572,73 +130,20 @@ def try_function(func: Callable[[], Optional[T]]) -> Optional[T]:
|
|
|
572
130
|
3. **Meaningful Messages**: Explain what happened and how to fix it
|
|
573
131
|
4. **Log Appropriately**: Error = log, expected failure = don't spam logs
|
|
574
132
|
5. **Handle at Right Level**: Catch where you can meaningfully handle
|
|
575
|
-
6. **Clean Up Resources**: Use try-finally, context managers, defer
|
|
133
|
+
6. **Clean Up Resources**: Use try-finally, context managers, defer, RAII
|
|
576
134
|
7. **Don't Swallow Errors**: Log or re-throw, don't silently ignore
|
|
577
135
|
8. **Type-Safe Errors**: Use typed errors when possible
|
|
578
136
|
|
|
579
|
-
```python
|
|
580
|
-
# Good error handling example
|
|
581
|
-
def process_order(order_id: str) -> Order:
|
|
582
|
-
"""Process order with comprehensive error handling."""
|
|
583
|
-
try:
|
|
584
|
-
# Validate input
|
|
585
|
-
if not order_id:
|
|
586
|
-
raise ValidationError("Order ID is required")
|
|
587
|
-
|
|
588
|
-
# Fetch order
|
|
589
|
-
order = db.get_order(order_id)
|
|
590
|
-
if not order:
|
|
591
|
-
raise NotFoundError("Order", order_id)
|
|
592
|
-
|
|
593
|
-
# Process payment
|
|
594
|
-
try:
|
|
595
|
-
payment_result = payment_service.charge(order.total)
|
|
596
|
-
except PaymentServiceError as e:
|
|
597
|
-
# Log and wrap external service error
|
|
598
|
-
logger.error(f"Payment failed for order {order_id}: {e}")
|
|
599
|
-
raise ExternalServiceError(
|
|
600
|
-
f"Payment processing failed",
|
|
601
|
-
service="payment_service",
|
|
602
|
-
details={"order_id": order_id, "amount": order.total}
|
|
603
|
-
) from e
|
|
604
|
-
|
|
605
|
-
# Update order
|
|
606
|
-
order.status = "completed"
|
|
607
|
-
order.payment_id = payment_result.id
|
|
608
|
-
db.save(order)
|
|
609
|
-
|
|
610
|
-
return order
|
|
611
|
-
|
|
612
|
-
except ApplicationError:
|
|
613
|
-
# Re-raise known application errors
|
|
614
|
-
raise
|
|
615
|
-
except Exception as e:
|
|
616
|
-
# Log unexpected errors
|
|
617
|
-
logger.exception(f"Unexpected error processing order {order_id}")
|
|
618
|
-
raise ApplicationError(
|
|
619
|
-
"Order processing failed",
|
|
620
|
-
code="INTERNAL_ERROR"
|
|
621
|
-
) from e
|
|
622
|
-
```
|
|
623
|
-
|
|
624
137
|
## Common Pitfalls
|
|
625
138
|
|
|
626
|
-
- **Catching Too Broadly**: `except Exception` hides bugs
|
|
139
|
+
- **Catching Too Broadly**: `except Exception` / `catch (e)` hides bugs
|
|
627
140
|
- **Empty Catch Blocks**: Silently swallowing errors
|
|
628
141
|
- **Logging and Re-throwing**: Creates duplicate log entries
|
|
629
142
|
- **Not Cleaning Up**: Forgetting to close files, connections
|
|
630
143
|
- **Poor Error Messages**: "Error occurred" is not helpful
|
|
631
|
-
- **
|
|
632
|
-
- **Ignoring Async Errors**: Unhandled promise rejections
|
|
144
|
+
- **Ignoring Async Errors**: Unhandled promise rejections, goroutine panics
|
|
633
145
|
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
- **references/exception-hierarchy-design.md**: Designing error class hierarchies
|
|
637
|
-
- **references/error-recovery-strategies.md**: Recovery patterns for different scenarios
|
|
638
|
-
- **references/async-error-handling.md**: Handling errors in concurrent code
|
|
639
|
-
- **assets/error-handling-checklist.md**: Review checklist for error handling
|
|
640
|
-
- **assets/error-message-guide.md**: Writing helpful error messages
|
|
641
|
-
- **scripts/error-analyzer.py**: Analyze error patterns in logs
|
|
146
|
+
---
|
|
642
147
|
|
|
643
148
|
## Error Architecture Interview
|
|
644
149
|
|