|
7 | 7 |
|
8 | 8 | from dataclasses import dataclass |
9 | 9 | from datetime import UTC, datetime, timedelta |
| 10 | +from enum import Enum |
| 11 | + |
| 12 | + |
| 13 | +class TerminationReason(Enum): |
| 14 | + """Reasons why a durable execution terminated.""" |
| 15 | + |
| 16 | + UNHANDLED_ERROR = "UNHANDLED_ERROR" |
| 17 | + INVOCATION_ERROR = "INVOCATION_ERROR" |
| 18 | + EXECUTION_ERROR = "EXECUTION_ERROR" |
| 19 | + CHECKPOINT_FAILED = "CHECKPOINT_FAILED" |
| 20 | + NON_DETERMINISTIC_EXECUTION = "NON_DETERMINISTIC_EXECUTION" |
| 21 | + STEP_INTERRUPTED = "STEP_INTERRUPTED" |
| 22 | + CALLBACK_ERROR = "CALLBACK_ERROR" |
| 23 | + SERIALIZATION_ERROR = "SERIALIZATION_ERROR" |
10 | 24 |
|
11 | 25 |
|
12 | 26 | class DurableExecutionsError(Exception): |
13 | 27 | """Base class for Durable Executions exceptions""" |
14 | 28 |
|
15 | 29 |
|
16 | | -class FatalError(DurableExecutionsError): |
17 | | - """Unrecoverable error. Will not retry.""" |
| 30 | +class UnrecoverableError(DurableExecutionsError): |
| 31 | + """Base class for errors that terminate execution.""" |
| 32 | + |
| 33 | + def __init__(self, message: str, termination_reason: TerminationReason): |
| 34 | + super().__init__(message) |
| 35 | + self.termination_reason = termination_reason |
| 36 | + |
| 37 | + |
| 38 | +class ExecutionError(UnrecoverableError): |
| 39 | + """Error that returns FAILED status without retry.""" |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + message: str, |
| 44 | + termination_reason: TerminationReason = TerminationReason.EXECUTION_ERROR, |
| 45 | + ): |
| 46 | + super().__init__(message, termination_reason) |
| 47 | + |
| 48 | + |
| 49 | +class InvocationError(UnrecoverableError): |
| 50 | + """Error that should cause Lambda retry by throwing from handler.""" |
| 51 | + |
| 52 | + def __init__( |
| 53 | + self, |
| 54 | + message: str, |
| 55 | + termination_reason: TerminationReason = TerminationReason.INVOCATION_ERROR, |
| 56 | + ): |
| 57 | + super().__init__(message, termination_reason) |
| 58 | + |
| 59 | + |
| 60 | +class CallbackError(ExecutionError): |
| 61 | + """Error in callback handling.""" |
| 62 | + |
| 63 | + def __init__(self, message: str, callback_id: str | None = None): |
| 64 | + super().__init__(message, TerminationReason.CALLBACK_ERROR) |
| 65 | + self.callback_id = callback_id |
| 66 | + |
| 67 | + |
| 68 | +class CheckpointFailedError(InvocationError): |
| 69 | + """Error when checkpoint operation fails.""" |
| 70 | + |
| 71 | + def __init__(self, message: str, step_id: str | None = None): |
| 72 | + super().__init__(message, TerminationReason.CHECKPOINT_FAILED) |
| 73 | + self.step_id = step_id |
| 74 | + |
| 75 | + |
| 76 | +class NonDeterministicExecutionError(ExecutionError): |
| 77 | + """Error when execution is non-deterministic.""" |
18 | 78 |
|
| 79 | + def __init__(self, message: str, step_id: str | None = None): |
| 80 | + super().__init__(message, TerminationReason.NON_DETERMINISTIC_EXECUTION) |
| 81 | + self.step_id = step_id |
19 | 82 |
|
20 | | -class CheckpointError(FatalError): |
| 83 | + |
| 84 | +class CheckpointError(CheckpointFailedError): |
21 | 85 | """Failure to checkpoint. Will terminate the lambda.""" |
22 | 86 |
|
| 87 | + def __init__(self, message: str): |
| 88 | + super().__init__(message) |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def from_exception(cls, exception: Exception) -> CheckpointError: |
| 92 | + return cls(message=str(exception)) |
| 93 | + |
23 | 94 |
|
24 | 95 | class ValidationError(DurableExecutionsError): |
25 | 96 | """Incorrect arguments to a Durable Function operation.""" |
@@ -50,9 +121,13 @@ def __init__( |
50 | 121 | self.stack_trace = stack_trace |
51 | 122 |
|
52 | 123 |
|
53 | | -class StepInterruptedError(UserlandError): |
| 124 | +class StepInterruptedError(InvocationError): |
54 | 125 | """Raised when a step is interrupted before it checkpointed at the end.""" |
55 | 126 |
|
| 127 | + def __init__(self, message: str, step_id: str | None = None): |
| 128 | + super().__init__(message, TerminationReason.STEP_INTERRUPTED) |
| 129 | + self.step_id = step_id |
| 130 | + |
56 | 131 |
|
57 | 132 | class SuspendExecution(BaseException): |
58 | 133 | """Raise this exception to suspend the current execution by returning PENDING to DAR. |
|
0 commit comments