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