Spaces:
Runtime error
Runtime error
File size: 2,690 Bytes
3821102 | 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 | """
Error response models
Standardized error responses for the API
"""
from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime
class ValidationError(BaseModel):
"""Single validation error"""
field: str = Field(..., description="Field that failed validation")
message: str = Field(..., description="Error message")
error_type: Optional[str] = Field(None, description="Type of validation error")
class ErrorResponse(BaseModel):
"""
Standard error response format
Used for all API errors
"""
success: bool = Field(default=False, description="Always false for errors")
error_code: Optional[str] = Field(
None, description="Application-specific error code"
)
message: str = Field(..., description="Human-readable error message")
details: Optional[str] = Field(None, description="Additional error details")
timestamp: datetime = Field(
default_factory=datetime.now, description="Error timestamp"
)
path: Optional[str] = Field(None, description="Request path that caused the error")
model_config = {
"json_schema_extra": {
"example": {
"success": False,
"error_code": "USER_NOT_FOUND",
"message": "User with ID '123' not found",
"timestamp": "2024-01-15T10:30:00",
"path": "/api/v1/users/123",
}
}
}
class ValidationErrorResponse(BaseModel):
"""
Validation error response
Used when Pydantic validation fails
"""
success: bool = Field(default=False)
error_code: str = Field(default="VALIDATION_ERROR")
message: str = Field(default="Validation error")
errors: List[ValidationError] = Field(..., description="List of validation errors")
timestamp: datetime = Field(default_factory=datetime.now)
model_config = {
"json_schema_extra": {
"example": {
"success": False,
"error_code": "VALIDATION_ERROR",
"message": "Validation error",
"errors": [
{
"field": "email",
"message": "value is not a valid email address",
"error_type": "value_error.email",
},
{
"field": "password",
"message": "Password must contain at least one uppercase letter",
"error_type": "value_error",
},
],
"timestamp": "2024-01-15T10:30:00",
}
}
}
|