Files
webref/backend/app/core/errors.py

69 lines
2.0 KiB
Python

"""Custom exception classes."""
from typing import Any
class WebRefException(Exception):
"""Base exception for all custom exceptions."""
def __init__(self, message: str, status_code: int = 500, details: dict[str, Any] | None = None):
self.message = message
self.status_code = status_code
self.details = details or {}
super().__init__(self.message)
class ValidationError(WebRefException):
"""Validation error."""
def __init__(self, message: str, details: dict[str, Any] | None = None):
super().__init__(message, status_code=422, details=details)
class AuthenticationError(WebRefException):
"""Authentication error."""
def __init__(self, message: str = "Authentication failed"):
super().__init__(message, status_code=401)
class AuthorizationError(WebRefException):
"""Authorization error."""
def __init__(self, message: str = "Insufficient permissions"):
super().__init__(message, status_code=403)
class NotFoundError(WebRefException):
"""Resource not found error."""
def __init__(self, resource: str, resource_id: str | None = None):
message = f"{resource} not found"
if resource_id:
message = f"{resource} with id {resource_id} not found"
super().__init__(message, status_code=404)
class ConflictError(WebRefException):
"""Resource conflict error."""
def __init__(self, message: str):
super().__init__(message, status_code=409)
class FileTooLargeError(WebRefException):
"""File size exceeds limit."""
def __init__(self, max_size: int):
message = f"File size exceeds maximum allowed size of {max_size} bytes"
super().__init__(message, status_code=413)
class UnsupportedFileTypeError(WebRefException):
"""Unsupported file type."""
def __init__(self, file_type: str, allowed_types: list[str]):
message = f"File type '{file_type}' not supported. Allowed types: {', '.join(allowed_types)}"
super().__init__(message, status_code=415)