-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp_exceptions.py
More file actions
248 lines (167 loc) · 7.53 KB
/
Copy pathhttp_exceptions.py
File metadata and controls
248 lines (167 loc) · 7.53 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
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
from fastapi import HTTPException, status
class RateLimitError(HTTPException):
"""Raised when a client exceeds the allowed request rate."""
def __init__(self, retry_after: int = 60):
self.retry_after = retry_after
super().__init__(
status_code=429, detail="Too many attempts. Please try again later."
)
class EmailAlreadyRegisteredError(HTTPException):
def __init__(self):
super().__init__(status_code=409, detail="This email is already registered")
class CredentialsError(HTTPException):
def __init__(self, message: str = "Invalid credentials"):
super().__init__(status_code=401, detail=message)
class AuthenticationError(HTTPException):
def __init__(self):
super().__init__(
status_code=status.HTTP_303_SEE_OTHER, headers={"Location": "/login"}
)
class AlreadyAuthenticatedError(HTTPException):
"""Raised when an authenticated user tries to access a page meant for unauthenticated users."""
def __init__(self):
super().__init__(
status_code=status.HTTP_303_SEE_OTHER, headers={"Location": "/dashboard/"}
)
class PasswordValidationError(HTTPException):
def __init__(self, field: str, message: str):
super().__init__(status_code=422, detail={"field": field, "message": message})
class InsufficientPermissionsError(HTTPException):
def __init__(
self,
message: str = "You don't have permission to perform this action",
):
super().__init__(status_code=403, detail=message)
class OrganizationSetupError(HTTPException):
def __init__(self, message: str = "Organization setup failed"):
super().__init__(status_code=500, detail=message)
class OrganizationNameTakenError(HTTPException):
def __init__(self):
super().__init__(status_code=400, detail="Organization name already taken")
class OrganizationNotFoundError(HTTPException):
def __init__(self):
super().__init__(status_code=404, detail="Organization not found")
class UserNotFoundError(HTTPException):
def __init__(self):
super().__init__(status_code=404, detail="User not found")
class UserAlreadyMemberError(HTTPException):
def __init__(self):
super().__init__(
status_code=400, detail="User is already a member of this organization"
)
class InvalidPermissionError(HTTPException):
"""Raised when a user attempts to assign an invalid permission to a role"""
def __init__(self, permission: str):
super().__init__(status_code=400, detail=f"Invalid permission: {permission}")
class RoleAlreadyExistsError(HTTPException):
"""Raised when attempting to create a role with a name that already exists"""
def __init__(self):
super().__init__(status_code=400, detail="Role already exists")
class RoleNotFoundError(HTTPException):
"""Raised when a requested role does not exist"""
def __init__(self):
super().__init__(status_code=404, detail="Role not found")
class RoleHasUsersError(HTTPException):
"""Raised when a requested role to be deleted has users"""
def __init__(self):
super().__init__(
status_code=400,
detail="Role cannot be deleted until users with that role are reassigned",
)
class CannotModifyDefaultRoleError(HTTPException):
"""Raised when attempting to modify or delete a default system role."""
def __init__(self, action: str = "modify"):
super().__init__(
status_code=403, detail=f"Default system roles cannot be {action}d."
)
class DataIntegrityError(HTTPException):
def __init__(self, resource: str = "Database resource"):
super().__init__(
status_code=500,
detail=(
f"{resource} is in a broken state; please contact a system administrator"
),
)
class InvalidImageError(HTTPException):
"""Raised when an invalid image is uploaded"""
def __init__(self, message: str = "Invalid image file"):
super().__init__(status_code=400, detail=message)
# --- Invitation-specific Errors ---
class UserIsAlreadyMemberError(HTTPException):
"""Raised when trying to invite a user who is already a member of the organization."""
def __init__(self):
super().__init__(
status_code=409, detail="This user is already a member of the organization."
)
class InvalidRoleForOrganizationError(HTTPException):
"""Raised when a role provided does not belong to the target organization.
Note: If the role ID simply doesn't exist, a standard 404 RoleNotFoundError should be raised.
"""
def __init__(self):
super().__init__(
status_code=400,
detail="The selected role does not belong to this organization.",
)
class InvitationEmailSendError(HTTPException):
"""Raised when the invitation email fails to send."""
def __init__(self):
super().__init__(
status_code=500, # Internal Server Error seems appropriate
detail="Failed to send invitation email. Please try again later or contact support.",
)
class InvitationNotFoundError(HTTPException):
"""Raised when an invitation ID does not exist."""
def __init__(self):
super().__init__(status_code=404, detail="Invitation not found")
class InvalidInvitationTokenError(HTTPException):
"""Raised when an invitation token is missing, superseded, or already used."""
def __init__(self):
super().__init__(
status_code=404,
detail=(
"This invitation link is no longer valid. "
"If you were invited again recently, use the link from the "
"most recent invitation email."
),
)
class ExpiredInvitationTokenError(HTTPException):
"""Raised when an invitation token exists but has passed its expiry date."""
def __init__(self):
super().__init__(
status_code=404,
detail=(
"This invitation link has expired. Ask your organization "
"administrator to send a new invitation, then use the link in "
"the latest email."
),
)
class InvitationEmailMismatchError(HTTPException):
"""Raised when a user attempts to accept an invitation sent to a different email address."""
def __init__(self):
super().__init__(
status_code=403,
detail="This invitation was sent to a different email address",
)
class MaxEmailsReachedError(HTTPException):
"""Raised when an account already has the maximum number of email addresses."""
def __init__(self):
super().__init__(
status_code=400, detail="Maximum number of email addresses reached"
)
class EmailNotVerifiedError(HTTPException):
"""Raised when attempting to promote an unverified email address."""
def __init__(self):
super().__init__(status_code=400, detail="Email address is not verified")
class CannotRemovePrimaryEmailError(HTTPException):
"""Raised when attempting to remove the primary email address."""
def __init__(self):
super().__init__(status_code=400, detail="Cannot remove primary email address")
class InvitationProcessingError(HTTPException):
"""Raised when an error occurs during the processing of a valid invitation."""
def __init__(
self, detail: str = "Failed to process invitation. Please try again later."
):
super().__init__(
status_code=500, # Internal Server Error
detail=detail,
)