Skip to content

Commit

Permalink
Added add_error_message().
Browse files Browse the repository at this point in the history
  • Loading branch information
adammansfield committed Sep 26, 2014
1 parent 0c3e1ed commit 1c4cfa7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
15 changes: 15 additions & 0 deletions powl/log.py
Expand Up @@ -132,6 +132,21 @@ def warning(self, message, *args, **kwargs):
self._logger.warning(message, *args, **kwargs)


def add_error_message(exception, message):
"""
Embeds an error message into an exception that can be retrieved by
try_get_error_message().
Parameters
----------
exception : Exception
message : str, ErrorMessage
"""
if isinstance(message, ErrorMessage):
exception.args += (message,)
else:
exception.args += (ErrorMessage(message),)

def try_get_error_message(exception):
"""
Trys to get an error message embedded in an exception.
Expand Down
24 changes: 17 additions & 7 deletions test/log.test.py
Expand Up @@ -3,22 +3,32 @@
import unittest
from powl import log

class TestTryGetErrorMessage(unittest.TestCase):
class TestErrorMessageFunctions(unittest.TestCase):
"""
Class for testing get_error_message().
"""

def test_returns_error_message(self):
def test_sanity_with_error_message(self):
"""
Test to ensure it will return the message in the exception.
Test that added message by error message can be retrieved.
"""
message = log.ErrorMessage("error message")
error_message = log.ErrorMessage("error message")
exception = Exception()
exception.args += (message,)
log.add_error_message(exception, error_message)
actual = log.try_get_error_message(exception)
self.assertEqual(str(message), actual)
self.assertEqual(str(error_message), actual)

def test_does_not_throw_if_no_error_message(self):
def test_sanity_with_string(self):
"""
Test that added message by string can be retrieved.
"""
error_message = "error message"
exception = Exception()
log.add_error_message(exception, error_message)
actual = log.try_get_error_message(exception)
self.assertEqual(error_message, actual)

def test__get__does_not_throw_if_no_error_message(self):
"""
Test to ensure that empty ErrorMessage is returned if none found.
"""
Expand Down

0 comments on commit 1c4cfa7

Please sign in to comment.