-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexceptions.py
More file actions
49 lines (30 loc) · 1.54 KB
/
Copy pathexceptions.py
File metadata and controls
49 lines (30 loc) · 1.54 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
"""Shared application exceptions."""
from fastapi import HTTPException, status
class ConflictError(HTTPException):
"""Raised when a resource already exists."""
def __init__(self, detail: str) -> None:
super().__init__(status_code=status.HTTP_409_CONFLICT, detail=detail)
class UnauthorizedError(HTTPException):
"""Raised when authentication fails."""
def __init__(self, detail: str = "Invalid credentials") -> None:
super().__init__(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=detail,
headers={"WWW-Authenticate": "Bearer"},
)
class ValidationError(HTTPException):
"""Raised when submitted data fails business validation."""
def __init__(self, detail: str) -> None:
super().__init__(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=detail)
class NotFoundError(HTTPException):
"""Raised when a requested resource does not exist."""
def __init__(self, detail: str) -> None:
super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail=detail)
class RateLimitError(HTTPException):
"""Raised when an auth endpoint rate limit is exceeded."""
def __init__(self, detail: str = "Too many requests") -> None:
super().__init__(status_code=status.HTTP_429_TOO_MANY_REQUESTS, detail=detail)
class ForbiddenError(HTTPException):
"""Raised when an authenticated user lacks permission."""
def __init__(self, detail: str = "Forbidden") -> None:
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)