-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexceptions.mdc
More file actions
66 lines (53 loc) · 1.8 KB
/
exceptions.mdc
File metadata and controls
66 lines (53 loc) · 1.8 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
---
description: Rules for exception handling and custom exceptions
globs: ["exceptions/**/*.py", "**/*.py"]
alwaysApply: true
---
# Exceptions Guidelines
## Exception Locations
**Gateway exceptions** (`exceptions/exceptions.py`):
- `MissingArgumentsException`, `ProjectAccessError`, `ServiceRequestsError`
- `DatabaseSessionError`, `AuthManagerError`, `EmbeddingConnectorError`
**Submodule exceptions**:
```python
from submodules.model.exceptions import EntityNotFoundException, EntityAlreadyExistsException
```
## Usage Patterns
**Raising exceptions:**
```python
# Validation
if not name:
raise MissingArgumentsException("Project name is required")
# Not found
proj = project.get(project_id)
if not proj:
raise EntityNotFoundException(f"Project {project_id} not found")
# Business logic
if not has_access(user_id, project_id):
raise ProjectAccessError(f"User {user_id} does not have access")
```
**Handling in routes:**
```python
try:
result = manager.operation(project_id)
return pack_json_result(result)
except EntityNotFoundException as e:
return pack_json_result({"error": str(e)}, status_code=404)
except ProjectAccessError as e:
return pack_json_result({"error": str(e)}, status_code=403)
except Exception as e:
logger.error(f"Error: {e}", exc_info=True)
return GENERIC_FAILURE_RESPONSE
```
## HTTP Status Code Mapping
- `400`: `ValueError`, `MissingArgumentsException`
- `403`: `ProjectAccessError`
- `404`: `EntityNotFoundException`
- `409`: `EntityAlreadyExistsException`
- `500`: `ServiceRequestsError`, `DatabaseSessionError`
## Best Practices
1. Use specific exception types, not generic `Exception`
2. Provide clear error messages with context
3. Log exceptions before raising or handling
4. Map exceptions to appropriate HTTP status codes
5. Don't swallow exceptions silently