class PermissionError_v1
Custom exception class raised when a user lacks the necessary permissions to perform a specific action in the CDocs system.
/tf/active/vicechatdev/CDocs/controllers/__init__.py
60 - 62
simple
Purpose
This exception class serves as a domain-specific error type for permission-related failures in the CDocs application. It extends Python's built-in Exception class to provide semantic clarity when authorization checks fail. By using a custom exception type, the application can distinguish permission errors from other types of exceptions and handle them appropriately (e.g., returning 403 Forbidden responses in web contexts). This exception is typically raised by permission checking decorators or authorization functions when validating user access to documents, reviews, approvals, or other protected resources.
Source Code
class PermissionError(Exception):
"""Exception raised when user doesn't have permission for an action."""
pass
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Exception | - |
Parameter Details
message: Optional error message string describing the specific permission violation. Inherited from Exception base class. Can include details about what action was attempted and why it was denied.
args: Variable positional arguments passed to the Exception constructor. Typically used to pass the error message and any additional context about the permission failure.
Return Value
Instantiation returns a PermissionError exception object that can be raised to signal permission violations. When raised and not caught, it will propagate up the call stack. The exception object contains the message and any additional arguments passed during instantiation, accessible via the args attribute.
Class Interface
Methods
__init__(*args, **kwargs)
Purpose: Initialize the PermissionError exception instance. Inherited from Exception base class.
Parameters:
args: Variable positional arguments, typically the error message string as the first argumentkwargs: Variable keyword arguments passed to the Exception constructor
Returns: None (constructor)
__str__() -> str
Purpose: Return string representation of the exception. Inherited from Exception base class.
Returns: String representation of the exception, typically the error message
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
args |
tuple | Tuple containing the arguments passed to the exception constructor, typically including the error message. Inherited from Exception base class. | instance |
Required Imports
from CDocs.exceptions import PermissionError
Usage Example
# Raising the exception
from CDocs.exceptions import PermissionError
def delete_document(user, document_id):
if not user.has_permission('delete_document'):
raise PermissionError(f"User {user.username} does not have permission to delete document {document_id}")
# Proceed with deletion
pass
# Catching the exception
try:
delete_document(current_user, doc_id)
except PermissionError as e:
print(f"Access denied: {e}")
# Handle permission error (e.g., return 403 response)
# Using with decorator pattern (common in CDocs)
def require_permission(permission_name):
def decorator(func):
def wrapper(*args, **kwargs):
if not check_permission(permission_name):
raise PermissionError(f"Permission '{permission_name}' required")
return func(*args, **kwargs)
return wrapper
return decorator
Best Practices
- Always provide a descriptive error message when raising PermissionError to help with debugging and user feedback
- Catch PermissionError at appropriate boundaries (e.g., API endpoints, view functions) to convert to user-friendly responses
- Use this exception consistently throughout the application for all permission-related failures to maintain clear error handling patterns
- Consider logging permission errors for security auditing purposes before handling them
- Do not expose sensitive information about system internals in the error message that could aid attackers
- This exception should be raised after permission checks fail, not for authentication failures (use a separate AuthenticationError for that)
- When catching this exception, ensure proper HTTP status codes are returned (403 Forbidden for permission errors vs 401 Unauthorized for authentication errors)
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class PermissionError 91.1% similar
-
class ResourceNotFoundError_v1 70.6% similar
-
class ValidationError 69.8% similar
-
class ControllerError 68.0% similar
-
class BusinessRuleError 65.7% similar