Base exception for all dirty arbiter errors.
| 10 | |
| 11 | |
| 12 | class DirtyError(Exception): |
| 13 | """Base exception for all dirty arbiter errors.""" |
| 14 | |
| 15 | def __init__(self, message, details=None): |
| 16 | self.message = message |
| 17 | self.details = details or {} |
| 18 | super().__init__(message) |
| 19 | |
| 20 | def __str__(self): |
| 21 | if self.details: |
| 22 | return f"{self.message}: {self.details}" |
| 23 | return self.message |
| 24 | |
| 25 | def to_dict(self): |
| 26 | """Serialize error for protocol transmission.""" |
| 27 | return { |
| 28 | "error_type": self.__class__.__name__, |
| 29 | "message": self.message, |
| 30 | "details": self.details, |
| 31 | } |
| 32 | |
| 33 | @classmethod |
| 34 | def from_dict(cls, data): |
| 35 | """Deserialize error from protocol transmission. |
| 36 | |
| 37 | Creates an error instance from a serialized dict. The returned |
| 38 | error will be an instance of the appropriate subclass based on |
| 39 | the error_type field, but constructed using the base DirtyError |
| 40 | __init__ to preserve all details. |
| 41 | """ |
| 42 | error_classes = { |
| 43 | "DirtyError": DirtyError, |
| 44 | "DirtyTimeoutError": DirtyTimeoutError, |
| 45 | "DirtyConnectionError": DirtyConnectionError, |
| 46 | "DirtyWorkerError": DirtyWorkerError, |
| 47 | "DirtyAppError": DirtyAppError, |
| 48 | "DirtyAppNotFoundError": DirtyAppNotFoundError, |
| 49 | "DirtyNoWorkersAvailableError": DirtyNoWorkersAvailableError, |
| 50 | "DirtyProtocolError": DirtyProtocolError, |
| 51 | } |
| 52 | error_type = data.get("error_type", "DirtyError") |
| 53 | error_class = error_classes.get(error_type, DirtyError) |
| 54 | |
| 55 | # Create instance and set attributes directly to bypass |
| 56 | # subclass __init__ complexity while preserving error type |
| 57 | error = Exception.__new__(error_class) |
| 58 | error.message = data.get("message", "Unknown error") |
| 59 | error.details = data.get("details") or {} |
| 60 | Exception.__init__(error, error.message) |
| 61 | |
| 62 | # Set subclass-specific attributes from details |
| 63 | if error_class == DirtyTimeoutError: |
| 64 | error.timeout = error.details.get("timeout") |
| 65 | elif error_class == DirtyConnectionError: |
| 66 | error.socket_path = error.details.get("socket_path") |
| 67 | elif error_class == DirtyWorkerError: |
| 68 | error.worker_id = error.details.get("worker_id") |
| 69 | error.traceback = error.details.get("traceback") |
no outgoing calls