Skip to content

Commit 6eec69a

Browse files
committed
MaxWhileTries: Optionally store an exception
So that we can react based on an exception raised while retrying whatever action ended up not succeeding. Signed-off-by: Zack Cerza <zack@cerza.org>
1 parent c433f10 commit 6eec69a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

teuthology/contextutil.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import logging
44
import time
55

6+
from typing import Optional
7+
68
from teuthology.config import config
79
from teuthology.exceptions import MaxWhileTries
810

@@ -120,7 +122,7 @@ def _make_error_msg(self):
120122
)
121123
return msg
122124

123-
def __call__(self):
125+
def __call__(self, last_exception: Optional[Exception] = None):
124126
self.counter += 1
125127
if self.counter == 1:
126128
return True
@@ -131,7 +133,7 @@ def must_stop():
131133
(self.timeout == 0 and must_stop())):
132134
error_msg = self._make_error_msg()
133135
if self._raise:
134-
raise MaxWhileTries(error_msg)
136+
raise MaxWhileTries(error_msg, last_exception)
135137
else:
136138
log.warning(error_msg)
137139
return False

teuthology/exceptions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
class BranchNotFoundError(ValueError):
24
def __init__(self, branch, repo=None):
35
self.branch = branch
@@ -200,7 +202,14 @@ class SkipJob(Exception):
200202

201203

202204
class MaxWhileTries(Exception):
203-
pass
205+
def __init__(self, message: str = '', last_exception: Optional[Exception] = None):
206+
self.message = message
207+
self.last_exception = last_exception
208+
209+
def __str__(self):
210+
if self.last_exception:
211+
return f"{self.message} (last exception: {self.last_exception})"
212+
return self.message
204213

205214

206215
class ConsoleError(Exception):

0 commit comments

Comments
 (0)