An Attempt encapsulates a call to a target function that may end as a normal return value from the function or an Exception depending on what occurred during the execution.
| 223 | |
| 224 | |
| 225 | class Attempt(object): |
| 226 | """ |
| 227 | An Attempt encapsulates a call to a target function that may end as a |
| 228 | normal return value from the function or an Exception depending on what |
| 229 | occurred during the execution. |
| 230 | """ |
| 231 | |
| 232 | def __init__(self, value, attempt_number, has_exception): |
| 233 | self.value = value |
| 234 | self.attempt_number = attempt_number |
| 235 | self.has_exception = has_exception |
| 236 | |
| 237 | def get(self, wrap_exception=False): |
| 238 | """ |
| 239 | Return the return value of this Attempt instance or raise an Exception. |
| 240 | If wrap_exception is true, this Attempt is wrapped inside of a |
| 241 | RetryError before being raised. |
| 242 | """ |
| 243 | if self.has_exception: |
| 244 | if wrap_exception: |
| 245 | raise RetryError(self) |
| 246 | else: |
| 247 | six.reraise(self.value[0], self.value[1], self.value[2]) |
| 248 | else: |
| 249 | return self.value |
| 250 | |
| 251 | def __repr__(self): |
| 252 | if self.has_exception: |
| 253 | return "Attempts: {0}, Error:\n{1}".format(self.attempt_number, "".join(traceback.format_tb(self.value[2]))) |
| 254 | else: |
| 255 | return "Attempts: {0}, Value: {1}".format(self.attempt_number, self.value) |
| 256 | |
| 257 | |
| 258 | class RetryError(Exception): |
no outgoing calls
no test coverage detected
searching dependent graphs…