fix: improve error handling and validation
- Add centralized exception classes - Validate signature request status before signing - Check contract status before approval/rejection - Add exception handlers to FastAPI app - Update tests for new validation logic Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
656f596d7e
commit
ae72b180e5
6 files changed
+194
-9
No files matched your search
@@ -0,0 +1,29 @@
|
||||
"""Exception handlers for FastAPI."""
|
||||
from fastapi import Request, status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.core.exceptions import AppException
|
||||
|
||||
|
||||
async def app_exception_handler(request: Request, exc: AppException):
|
||||
"""Handle application exceptions."""
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
content={
|
||||
"code": exc.code,
|
||||
"message": exc.message,
|
||||
"details": exc.details,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def generic_exception_handler(request: Request, exc: Exception):
|
||||
"""Handle generic exceptions."""
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
content={
|
||||
"code": 50000,
|
||||
"message": "Internal server error",
|
||||
"details": {},
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,87 @@
|
||||
"""Core exceptions for the application."""
|
||||
|
||||
|
||||
class AppException(Exception):
|
||||
"""Base application exception."""
|
||||
|
||||
def __init__(self, code: int, message: str, details: dict = None):
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.details = details or {}
|
||||
|
||||
|
||||
class NotFoundError(AppException):
|
||||
"""Resource not found exception."""
|
||||
|
||||
def __init__(self, resource: str, resource_id: int = None):
|
||||
super().__init__(
|
||||
code=10002,
|
||||
message=f"{resource} not found",
|
||||
details={"resource": resource, "id": resource_id}
|
||||
)
|
||||
|
||||
|
||||
class ValidationError(AppException):
|
||||
"""Validation error exception."""
|
||||
|
||||
def __init__(self, message: str, details: dict = None):
|
||||
super().__init__(code=10001, message=message, details=details)
|
||||
|
||||
|
||||
class AuthenticationError(AppException):
|
||||
"""Authentication error exception."""
|
||||
|
||||
def __init__(self, message: str = "Authentication failed"):
|
||||
super().__init__(code=20001, message=message)
|
||||
|
||||
|
||||
class AuthorizationError(AppException):
|
||||
"""Authorization error exception."""
|
||||
|
||||
def __init__(self, message: str = "Permission denied"):
|
||||
super().__init__(code=20002, message=message)
|
||||
|
||||
|
||||
class SignatureError(AppException):
|
||||
"""Signature related error exception."""
|
||||
|
||||
def __init__(self, message: str, details: dict = None):
|
||||
super().__init__(code=40000, message=message, details=details)
|
||||
|
||||
|
||||
class SignatureExpiredError(SignatureError):
|
||||
"""Signature request expired exception."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(code=40001, message="Signature request has expired")
|
||||
|
||||
|
||||
class SignatureAlreadySignedError(SignatureError):
|
||||
"""Signature already completed exception."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(code=40002, message="Signature request already completed")
|
||||
|
||||
|
||||
class ContractError(AppException):
|
||||
"""Contract related error exception."""
|
||||
|
||||
def __init__(self, message: str, details: dict = None):
|
||||
super().__init__(code=50000, message=message, details=details)
|
||||
|
||||
|
||||
class InvalidContractStatusError(ContractError):
|
||||
"""Invalid contract status for operation."""
|
||||
|
||||
def __init__(self, current_status: str, required_status: str):
|
||||
super().__init__(
|
||||
message=f"Invalid contract status: {current_status}, expected: {required_status}",
|
||||
details={"current_status": current_status, "required_status": required_status}
|
||||
)
|
||||
|
||||
|
||||
class LLMError(AppException):
|
||||
"""LLM service error exception."""
|
||||
|
||||
def __init__(self, message: str = "LLM service error"):
|
||||
super().__init__(code=30001, message=message)
|
||||
Reference in new issue
Block a user