Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: local
hooks:
- id: ruff-format
name: ruff format --check
entry: uv run ruff format --check
language: system
types: [python]
pass_filenames: false

- id: ruff-check
name: ruff check
entry: uv run ruff check
language: system
types: [python]
pass_filenames: false

- id: ty-check
name: ty check
entry: uv run ty check .
language: system
types: [python]
pass_filenames: false
3 changes: 2 additions & 1 deletion exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ def __init__(self, user: User, access_token: str, refresh_token: str):
# Define custom exception for email sending failure
class EmailSendFailedError(Exception):
"""Custom exception for email sending failures."""
pass

pass
126 changes: 48 additions & 78 deletions exceptions/http_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,115 +1,86 @@
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."
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"
)
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
)
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"}
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/"}
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
}
)
super().__init__(status_code=422, detail={"field": field, "message": message})


class InsufficientPermissionsError(HTTPException):
def __init__(self):
super().__init__(
status_code=403,
detail="You don't have permission to perform this action"
status_code=403, detail="You don't have permission to perform this action"
)


class OrganizationSetupError(HTTPException):
def __init__(self, message: str = "Organization setup failed"):
super().__init__(
status_code=500,
detail=message
)
super().__init__(status_code=500, detail=message)


class OrganizationNameTakenError(HTTPException):
def __init__(self):
super().__init__(
status_code=400,
detail="Organization name already taken"
)
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"
)
super().__init__(status_code=404, detail="Organization not found")


class UserNotFoundError(HTTPException):
def __init__(self):
super().__init__(
status_code=404,
detail="User not found"
)
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"
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}"
)
super().__init__(status_code=400, detail=f"Invalid permission: {permission}")


class RoleAlreadyExistsError(HTTPException):
Expand All @@ -132,29 +103,26 @@ class RoleHasUsersError(HTTPException):
def __init__(self):
super().__init__(
status_code=400,
detail="Role cannot be deleted until users with that role are reassigned"
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."
status_code=403, detail=f"Default system roles cannot be {action}d."
)


class DataIntegrityError(HTTPException):
def __init__(
self,
resource: str = "Database resource"
):
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"
)
),
)


Expand All @@ -167,93 +135,95 @@ def __init__(self, message: str = "Invalid image file"):

# --- 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."
status_code=409, detail="This user is already a member of the organization."
)


class ActiveInvitationExistsError(HTTPException):
"""Raised when trying to invite a user for whom an active invitation already exists."""

def __init__(self):
super().__init__(
status_code=409,
detail="An active invitation already exists for this email address in this organization."
detail="An active invitation already exists for this email address in this 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."
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."
status_code=500, # Internal Server Error seems appropriate
detail="Failed to send invitation email. Please try again later or contact support.",
)


class InvalidInvitationTokenError(HTTPException):
"""Raised when an invitation token is invalid, expired, or not found."""

def __init__(self):
super().__init__(
status_code=404,
detail="Invitation not found or expired"
)
super().__init__(status_code=404, detail="Invitation not found or expired")


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"
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"
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"
)
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"
)
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."):

def __init__(
self, detail: str = "Failed to process invitation. Please try again later."
):
super().__init__(
status_code=500, # Internal Server Error
detail=detail
)
status_code=500, # Internal Server Error
detail=detail,
)
Loading
Loading