class ValidationError_v1
Custom exception class that signals input validation failures in the controller layer of the CDocs application.
/tf/active/vicechatdev/CDocs/controllers/__init__.py
72 - 74
simple
Purpose
ValidationError is a specialized exception class that inherits from ControllerError. It is designed to be raised when input validation fails in controller operations, providing a clear and specific exception type for validation-related errors. This allows calling code to distinguish validation failures from other types of controller errors and handle them appropriately. It serves as a semantic marker in the exception hierarchy to improve error handling and debugging.
Source Code
class ValidationError(ControllerError):
"""Exception raised when input validation fails."""
pass
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
ControllerError | - |
Parameter Details
*args: Variable positional arguments passed to the parent Exception class, typically the error message string describing what validation failed
**kwargs: Variable keyword arguments passed to the parent Exception class for additional exception context
Return Value
Instantiation returns a ValidationError exception object that can be raised. When caught, it provides access to the error message and traceback information inherited from the base Exception class through ControllerError.
Class Interface
Methods
__init__(*args, **kwargs)
Purpose: Initialize the ValidationError exception with an error message and optional additional context
Parameters:
*args: Positional arguments, typically a string message describing the validation failure**kwargs: Keyword arguments for additional exception context
Returns: None (constructor)
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
args |
tuple | Tuple of arguments passed to the exception, typically containing the error message as the first element. Inherited from base Exception class. | instance |
Dependencies
CDocs.configCDocs.models.user_extensions
Required Imports
from CDocs.controllers.errors import ValidationError
Usage Example
from CDocs.controllers.errors import ValidationError
def validate_document_title(title: str) -> None:
"""Validate document title meets requirements."""
if not title:
raise ValidationError("Document title cannot be empty")
if len(title) > 255:
raise ValidationError("Document title exceeds maximum length of 255 characters")
if not title.strip():
raise ValidationError("Document title cannot contain only whitespace")
# Usage in controller
try:
validate_document_title("")
except ValidationError as e:
print(f"Validation failed: {e}")
# Handle validation error appropriately
# Catching in exception hierarchy
try:
validate_document_title("x" * 300)
except ControllerError as e:
# Catches ValidationError and other ControllerError subclasses
print(f"Controller error occurred: {e}")
Best Practices
- Always provide a descriptive error message when raising ValidationError to help identify what validation failed
- Use ValidationError specifically for input validation failures, not for other types of errors
- Catch ValidationError when you need to handle validation errors differently from other controller errors
- Consider catching the parent ControllerError if you want to handle all controller-related errors uniformly
- Include specific details about the validation failure in the error message (e.g., which field failed, what constraint was violated)
- Raise ValidationError early in controller methods after validating inputs to fail fast
- Document which validations can raise ValidationError in your controller method docstrings
- Use ValidationError in conjunction with validation logic to separate validation concerns from business logic
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ValidationError 92.0% similar
-
class ControllerError 65.3% similar
-
class PermissionError_v1 64.7% similar
-
class BusinessRuleError 63.8% similar
-
class IntegrationError 60.9% similar