Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry error message #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ Patches and Suggestions
- Jonathan Herriott
- Job Evers
- Cyrus Durgin
- Luca Castoro
16 changes: 10 additions & 6 deletions retrying.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def __init__(self,
wait_func=None,
wait_jitter_max=None,
before_attempts=None,
after_attempts=None):
after_attempts=None,
custom_message=None):

self._stop_max_attempt_number = 5 if stop_max_attempt_number is None else stop_max_attempt_number
self._stop_max_delay = 100 if stop_max_delay is None else stop_max_delay
Expand All @@ -92,6 +93,7 @@ def __init__(self,
self._wait_jitter_max = 0 if wait_jitter_max is None else wait_jitter_max
self._before_attempts = before_attempts
self._after_attempts = after_attempts
self._custom_message = custom_message

# TODO add chaining of stop behaviors
# stop behavior
Expand Down Expand Up @@ -217,15 +219,16 @@ def should_reject(self, attempt):
def call(self, fn, *args, **kwargs):
start_time = int(round(time.time() * 1000))
attempt_number = 1
message = self._custom_message if self._custom_message else fn.__name__
while True:
if self._before_attempts:
self._before_attempts(attempt_number)

try:
attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
attempt = Attempt(fn(*args, **kwargs), attempt_number, False, message)
except:
tb = sys.exc_info()
attempt = Attempt(tb, attempt_number, True)
attempt = Attempt(tb, attempt_number, True, message)

if not self.should_reject(attempt):
return attempt.get(self._wrap_exception)
Expand Down Expand Up @@ -257,10 +260,11 @@ class Attempt(object):
occurred during the execution.
"""

def __init__(self, value, attempt_number, has_exception):
def __init__(self, value, attempt_number, has_exception, message):
self.value = value
self.attempt_number = attempt_number
self.has_exception = has_exception
self.message = message

def get(self, wrap_exception=False):
"""
Expand All @@ -278,9 +282,9 @@ def get(self, wrap_exception=False):

def __repr__(self):
if self.has_exception:
return "Attempts: {0}, Error:\n{1}".format(self.attempt_number, "".join(traceback.format_tb(self.value[2])))
return "Attempts: {0}, {1}, Error:\n{2}".format(self.attempt_number, self.message, "".join(traceback.format_tb(self.value[2])))
else:
return "Attempts: {0}, Value: {1}".format(self.attempt_number, self.value)
return "Attempts: {0}, {1}, Value: {2}".format(self.attempt_number, self.message, self.value)


class RetryError(Exception):
Expand Down
27 changes: 27 additions & 0 deletions test_retrying.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## See the License for the specific language governing permissions and
## limitations under the License.

import sys
import time
import unittest

Expand Down Expand Up @@ -468,5 +469,31 @@ def _test_after():

self.assertTrue(TestBeforeAfterAttempts._attempt_number is 2)

class TestMeaningfulMessage(unittest.TestCase):

def test_function_name(self):

@retry(retry_on_result = lambda x: not x, stop_max_attempt_number = 5)
def just_a_function():
return 0

try:
just_a_function()
self.assertFalse(True)
except RetryError as ex:
self.assertTrue('just_a_function' in str(ex))

def test_custom_message(self):

@retry(retry_on_result = lambda x: not x, stop_max_attempt_number = 5, custom_message = 'hello world')
def just_a_function():
return 0

try:
just_a_function()
self.assertFalse(True)
except RetryError as ex:
self.assertTrue('hello world' in str(ex))

if __name__ == '__main__':
unittest.main()