diff options
author | Ivan Alejandro <ivanalejandro0@gmail.com> | 2013-07-25 11:02:18 -0300 |
---|---|---|
committer | Ivan Alejandro <ivanalejandro0@gmail.com> | 2013-07-25 11:05:44 -0300 |
commit | 7747b4c28c4d9ed596e72061f34261d6158efe17 (patch) | |
tree | 0d5df83213616c8cad4cdf948555d398aee68b58 | |
parent | f659de6e98073dc738b8d245e0ddb897f5bd3779 (diff) |
Add leap_check helper method, #3007 related issue.
-rw-r--r-- | changes/feature_add-leap-check | 1 | ||||
-rw-r--r-- | src/leap/common/check.py | 17 |
2 files changed, 18 insertions, 0 deletions
diff --git a/changes/feature_add-leap-check b/changes/feature_add-leap-check new file mode 100644 index 0000000..5906c03 --- /dev/null +++ b/changes/feature_add-leap-check @@ -0,0 +1 @@ + o Add leap_check helper method, to use whenever leap_assert does not apply. Related to #3007. diff --git a/src/leap/common/check.py b/src/leap/common/check.py index e6b0b52..82da5ff 100644 --- a/src/leap/common/check.py +++ b/src/leap/common/check.py @@ -60,3 +60,20 @@ def leap_assert_type(var, expectedType): leap_assert(isinstance(var, expectedType), "Expected type %r instead of %r" % (expectedType, type(var))) + + +def leap_check(condition, message="", exception=Exception): + """ + Asserts the condition and displays the message if that's not + met. It also logs the error and its backtrace. + + :param condition: condition to check + :type condition: bool + :param message: message to display if the condition isn't met + :type message: str + :param exception: the exception to raise in case of condition being false. + :type exception: Exception + """ + if not condition: + logger.error(message) + raise exception(message) |