Add error codes to resource limit exceptions for better programmatic handling:
class ResourceLimitError(ValueError):
def __init__(self, message: str, error_code: str, limit_type: str, actual: int, limit: int):
self.error_code = error_code # e.g., "FILE_SIZE_EXCEEDED"
self.limit_type = limit_type # e.g., "max_file_size"
self.actual = actual
self.limit = limit
super().__init__(message)
Benefits
- Easier to catch specific errors
- Better for API users and tools
- Enables automated retry with adjusted limits
Example Usage
try:
convert("large.psd", "output.svg")
except ResourceLimitError as e:
if e.error_code == "FILE_SIZE_EXCEEDED":
# Retry with larger limit
limits = ResourceLimits(max_file_size=e.actual + 100_000_000)
convert("large.psd", "output.svg", resource_limits=limits)
Considerations
- Breaking change (new exception type)
- Need comprehensive error code taxonomy
- Should maintain backward compatibility with ValueError/TimeoutError
- Consider using exception groups for multiple violations
Add error codes to resource limit exceptions for better programmatic handling:
Benefits
Example Usage
Considerations