class ValidationError
A custom exception class that is raised when input validation fails in the CDocs document management system.
/tf/active/vicechatdev/CDocs/controllers/admin_controller.py
30 - 32
simple
Purpose
ValidationError serves as a specialized exception type for signaling validation failures throughout the CDocs application. It extends Python's built-in Exception class to provide a semantic way to handle validation-related errors, making it easier to distinguish validation failures from other types of exceptions in error handling logic. This exception is typically raised when user input, document data, or configuration values fail to meet required validation criteria.
Source Code
class ValidationError(Exception):
"""Exception raised when input validation fails."""
pass
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Exception | - |
Parameter Details
message: Optional error message string that describes the validation failure. Inherited from Exception base class. Can be accessed via args[0] on the exception instance.
*args: Variable length argument list passed to the Exception base class constructor. Typically used to pass error messages and additional context.
**kwargs: Arbitrary keyword arguments passed to the Exception base class constructor.
Return Value
Instantiation returns a ValidationError exception object that can be raised using the 'raise' statement. The exception object contains the error message and any additional arguments passed during instantiation. When caught, the exception can be inspected for its message via str(exception) or exception.args.
Class Interface
Methods
__init__(*args, **kwargs)
Purpose: Initializes the ValidationError exception instance. Inherited from Exception base class.
Parameters:
*args: Variable length argument list, typically containing the error message as the first argument**kwargs: Arbitrary keyword arguments passed to the base Exception class
Returns: None (constructor)
__str__() -> str
Purpose: Returns a string representation of the exception. Inherited from Exception base class.
Returns: String representation of the exception, typically the error message
__repr__() -> str
Purpose: Returns a detailed string representation of the exception object. Inherited from Exception base class.
Returns: String showing the exception class name and arguments
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
args |
tuple | Tuple containing all arguments passed to the exception constructor. The first element (args[0]) typically contains the error message. | instance |
__cause__ |
Optional[BaseException] | The exception that directly caused this exception (set via 'raise ... from ...' syntax). Inherited from BaseException. | instance |
__context__ |
Optional[BaseException] | The exception context (previous exception during handling). Inherited from BaseException. | instance |
__traceback__ |
Optional[TracebackType] | The traceback object associated with this exception. Inherited from BaseException. | instance |
Required Imports
from CDocs import ValidationError
Usage Example
# Raising the exception
def validate_document_name(name):
if not name or len(name) < 3:
raise ValidationError("Document name must be at least 3 characters long")
if not name.isalnum():
raise ValidationError("Document name must contain only alphanumeric characters")
return True
# Catching and handling the exception
try:
validate_document_name("ab")
except ValidationError as e:
print(f"Validation failed: {e}")
# Output: Validation failed: Document name must be at least 3 characters long
# Using with additional context
try:
user_input = ""
if not user_input:
raise ValidationError("Empty input provided", {"field": "username", "value": user_input})
except ValidationError as e:
error_msg = e.args[0]
context = e.args[1] if len(e.args) > 1 else None
print(f"Error: {error_msg}, Context: {context}")
Best Practices
- Always provide a descriptive error message when raising ValidationError to help with debugging and user feedback
- Use ValidationError specifically for validation-related failures, not for other types of errors like database or network issues
- Catch ValidationError at appropriate levels in your application to provide user-friendly error messages
- Consider including additional context (field names, invalid values) as extra arguments when raising the exception
- Document which functions/methods may raise ValidationError in their docstrings
- Use specific validation error messages that clearly indicate what validation rule was violated
- Consider creating subclasses of ValidationError for different types of validation failures if needed (e.g., SchemaValidationError, InputValidationError)
- When catching ValidationError, log the error appropriately for debugging while showing sanitized messages to end users
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ValidationError_v1 92.0% similar
-
class PermissionError_v1 69.8% similar
-
class BusinessRuleError 66.3% similar
-
class PermissionError 65.0% similar
-
class ResourceNotFoundError_v1 64.1% similar