class PermissionError
Custom exception class that signals when a user attempts an action they lack permission to perform.
/tf/active/vicechatdev/CDocs/controllers/admin_controller.py
34 - 36
simple
Purpose
PermissionError is a custom exception class used throughout the CDocs application to handle authorization failures. It inherits from Python's base Exception class and is raised when a user attempts to perform an action (such as accessing, modifying, or deleting documents) without the necessary permissions. This exception allows the application to distinguish permission-related errors from other types of exceptions and handle them appropriately with specific error messages or HTTP status codes.
Source Code
class PermissionError(Exception):
"""Exception raised when a user lacks permission for an action."""
pass
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Exception | - |
Parameter Details
message: Optional string message describing the specific permission violation. Inherited from Exception base class. Can be accessed via args[0] on the exception instance.
*args: Variable positional arguments passed to the Exception base class constructor, typically used to provide error messages or additional context.
**kwargs: Variable keyword arguments passed to the Exception base class constructor for additional exception metadata.
Return Value
Instantiation returns a PermissionError exception object that can be raised to signal permission violations. The exception object contains the message and any additional arguments passed during instantiation, accessible through the args attribute.
Class Interface
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
args |
tuple | Tuple containing the arguments passed to the exception constructor, typically the error message as the first element. Inherited from Exception base class. | instance |
Required Imports
from CDocs.models.document import PermissionError
Usage Example
# Raising the exception with a message
from CDocs.models.document import PermissionError
def delete_document(user, document):
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
document.delete()
# Catching and handling the exception
try:
delete_document(current_user, my_document)
except PermissionError as e:
print(f"Access denied: {e}")
# Log the error or return HTTP 403 Forbidden
# Raising without a message
if not authorized:
raise PermissionError()
# Raising with multiple arguments
raise PermissionError("Access denied", user_id, resource_id)
Best Practices
- Always provide a descriptive error message when raising PermissionError to help with debugging and user feedback
- Catch PermissionError specifically when you need to handle authorization failures differently from other exceptions
- Use this exception consistently throughout the application for all permission-related failures to maintain uniform error handling
- Consider logging permission errors for security auditing purposes before re-raising or handling them
- In web applications, map PermissionError to HTTP 403 Forbidden status codes
- Include relevant context in the error message such as user identifier, resource identifier, and the attempted action
- Do not expose sensitive information in error messages that might be displayed to end users
- This exception does not maintain any state beyond the message; it is stateless and immutable once created
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class PermissionError_v1 91.1% similar
-
class ControllerError 68.4% similar
-
class ResourceNotFoundError_v1 68.0% similar
-
class BusinessRuleError 66.7% similar
-
class ResourceNotFoundError 65.7% similar