-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
30 lines (19 loc) · 731 Bytes
/
Copy pathexceptions.py
File metadata and controls
30 lines (19 loc) · 731 Bytes
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
from pydantic import ValidationError as PydanticValidationError
class AppError(Exception):
status_code = 400 # Default fallback
def __init__(self, errors: dict | str):
self.errors = errors
super().__init__(errors)
class ValidationError(AppError):
status_code = 400
class AuthenticationError(AppError):
status_code = 401
class AuthorizationError(AppError):
status_code = 403
class NotFoundError(AppError):
status_code = 404
class ConflictError(AppError):
status_code = 409
def format_pydantic_errors(e: PydanticValidationError) -> dict:
"""Turns Pydantic errors into a clean {field: message} dictionary."""
return {str(err["loc"][0]): err["msg"] for err in e.errors()}