From ec299a43ba206f4b5426dad9590d400d42ea3e28 Mon Sep 17 00:00:00 2001 From: Jeremy Stanley Date: Sun, 2 Jul 2017 15:11:17 +0000 Subject: [PATCH] Handle ValueError when setting incorrect type Trap for ValueError exceptions when attempting to pass a value to the set command with an incorrect data type for the facet being set, returning a clear failure message to the calling user. --- mudpy/misc.py | 16 +++++++++++----- mudpy/tests/selftest.py | 6 ++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/mudpy/misc.py b/mudpy/misc.py index 6cb7804..470fb96 100644 --- a/mudpy/misc.py +++ b/mudpy/misc.py @@ -2284,11 +2284,17 @@ def command_set(actor, parameters): if element not in universe.contents: message = "The \"" + element + "\" element does not exist." else: - universe.contents[element].set(facet, value) - message = ("You have successfully (re)set the \"" + facet - + "\" facet of element \"" + element - + "\". Try \"show element " + - element + "\" for verification.") + try: + universe.contents[element].set(facet, value) + except ValueError: + message = ("Value \"%s\" of type \"%s\" cannot be coerced " + "to the correct datatype for facet \"%s\"." % + (value, type(value), facet)) + else: + message = ("You have successfully (re)set the \"" + facet + + "\" facet of element \"" + element + + "\". Try \"show element " + + element + "\" for verification.") actor.send(message) diff --git a/mudpy/tests/selftest.py b/mudpy/tests/selftest.py index 56814d4..a6bb6d6 100644 --- a/mudpy/tests/selftest.py +++ b/mudpy/tests/selftest.py @@ -174,6 +174,11 @@ test_custom_loglevel = ( "[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""), ) +test_invalid_loglevel = ( + (2, "> ", "set account:admin loglevel two"), + (2, "Value \"two\" of type \"\" cannot be coerced .*> ", ""), +) + test_log_no_errors = ( (2, "> ", "show log 7"), (2, "None of the [0-9]+ lines in memory matches your request\.", ""), @@ -197,6 +202,7 @@ dialogue = ( (test_show_element, "show element"), (test_show_log, "show log"), (test_custom_loglevel, "show log"), + (test_invalid_loglevel, "invalid loglevel"), (test_log_no_errors, "no errors logged"), ) -- 2.11.0