class BusinessRuleError
Custom exception class that signals when a business rule has been violated in the CDocs document management system.
/tf/active/vicechatdev/CDocs/controllers/admin_controller.py
38 - 40
simple
Purpose
BusinessRuleError is a domain-specific exception used throughout the CDocs application to indicate that an operation cannot proceed because it would violate a business rule or constraint. This provides a clear semantic distinction from generic exceptions, allowing calling code to handle business logic violations differently from technical errors. Common use cases include validation failures, workflow constraint violations, permission checks, and document state transition rules.
Source Code
class BusinessRuleError(Exception):
"""Exception raised when a business rule is violated."""
pass
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Exception | - |
Parameter Details
message: Optional error message string describing the specific business rule that was violated. Inherited from the base Exception class. Can be accessed via args[0] on the exception instance.
*args: Variable positional arguments passed to the base Exception class constructor, typically used to provide error details.
**kwargs: Variable keyword arguments passed to the base Exception class constructor (though Exception typically doesn't use kwargs).
Return Value
Instantiation returns a BusinessRuleError exception object that can be raised. When caught, the exception object contains the error message and any additional arguments passed during construction. The exception inherits all methods and attributes from the base Exception class, including __str__(), __repr__(), and the args tuple.
Class Interface
Methods
__init__(*args, **kwargs)
Purpose: Initializes the BusinessRuleError exception instance with optional error message and arguments
Parameters:
*args: Variable positional arguments, typically a single string message describing the business rule violation**kwargs: Variable keyword arguments (inherited from Exception, rarely used)
Returns: None (constructor)
__str__() -> str
Purpose: Returns string representation of the exception (inherited from Exception)
Returns: String representation of the error message
__repr__() -> str
Purpose: Returns detailed string representation of the exception (inherited from Exception)
Returns: Detailed string representation including class name and arguments
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
args |
tuple | Tuple containing all arguments passed to the exception constructor, typically containing the error message as the first element. Inherited from Exception. | instance |
Required Imports
from CDocs.models.document import BusinessRuleError
Usage Example
# Raising the exception
def approve_document(doc_id, user):
if not user.has_permission('approve'):
raise BusinessRuleError(f"User {user.name} lacks approval permission")
if doc.status != 'pending':
raise BusinessRuleError(f"Document must be in 'pending' status, currently '{doc.status}'")
# proceed with approval
# Catching the exception
try:
approve_document('DOC-123', current_user)
except BusinessRuleError as e:
print(f"Business rule violation: {e}")
# Handle gracefully, perhaps return error to user
except Exception as e:
print(f"Unexpected error: {e}")
# Handle technical errors differently
Best Practices
- Always provide a descriptive error message when raising BusinessRuleError to help with debugging and user feedback
- Use BusinessRuleError specifically for business logic violations, not for technical errors like database connection failures
- Catch BusinessRuleError separately from generic exceptions to provide appropriate user-facing error messages
- Consider logging BusinessRuleError occurrences for audit and analytics purposes
- Document which business rules can trigger this exception in the docstrings of methods that raise it
- The exception can be instantiated with a simple string message: BusinessRuleError('message') or with multiple arguments
- Since this inherits from Exception, it can be used in standard try-except blocks and will be caught by bare 'except Exception' clauses
- Consider creating more specific subclasses of BusinessRuleError for different categories of business rules if the application grows complex
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class BusinessRuleError_v1 85.9% similar
-
class PermissionError 66.7% similar
-
class ValidationError 66.3% similar
-
class PermissionError_v1 65.7% similar
-
class ValidationError_v1 63.8% similar