From: Christian Heller Date: Sun, 14 Jul 2024 20:10:34 +0000 (+0200) Subject: Return NotFoundExceptions from InputsParser if no value found to key. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/error?a=commitdiff_plain;h=df5055099b1ef6acd094f6ace452cb25dca63b2b;p=plomtask Return NotFoundExceptions from InputsParser if no value found to key. --- diff --git a/plomtask/http.py b/plomtask/http.py index b3b9d7a..8752600 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -78,14 +78,14 @@ class InputsParser: def __init__(self, dict_: dict[str, list[str]], strictness: bool = True) -> None: self.inputs = dict_ - self.strict = strictness + self.strict = strictness # return None on absence of key, or fail? def get_str(self, key: str, default: str = '', ignore_strict: bool = False) -> str: """Retrieve single/first string value of key, or default.""" if key not in self.inputs.keys() or 0 == len(self.inputs[key]): if self.strict and not ignore_strict: - raise BadFormatException(f'no value found for key {key}') + raise NotFoundException(f'no value found for key {key}') return default return self.inputs[key][0] @@ -560,9 +560,12 @@ class TaskHandler(BaseHTTPRequestHandler): def do_POST_day(self) -> str: """Update or insert Day of date and Todos mapped to it.""" # pylint: disable=too-many-locals - date = self._params.get_str('date') - day_comment = self._form_data.get_str('day_comment') - make_type = self._form_data.get_str('make_type') + try: + date = self._params.get_str('date') + day_comment = self._form_data.get_str('day_comment') + make_type = self._form_data.get_str('make_type') + except NotFoundException as e: + raise BadFormatException(e) from e old_todos = self._form_data.get_all_int('todo_id') new_todos = self._form_data.get_all_int('new_todo') comments = self._form_data.get_all_str('comment') @@ -666,9 +669,12 @@ class TaskHandler(BaseHTTPRequestHandler): """Update or insert Process of ?id= and fields defined in postvars.""" # pylint: disable=too-many-locals # pylint: disable=too-many-statements - title = self._form_data.get_str('title') - description = self._form_data.get_str('description') - effort = self._form_data.get_float('effort') + try: + title = self._form_data.get_str('title') + description = self._form_data.get_str('description') + effort = self._form_data.get_float('effort') + except NotFoundException as e: + raise BadFormatException from e conditions = self._form_data.get_all_int('conditions') blockers = self._form_data.get_all_int('blockers') enables = self._form_data.get_all_int('enables') @@ -751,9 +757,12 @@ class TaskHandler(BaseHTTPRequestHandler): @_delete_or_post(Condition, '/conditions') def do_POST_condition(self, condition: Condition) -> str: """Update/insert Condition of ?id= and fields defined in postvars.""" - is_active = self._form_data.get_str('is_active') == 'True' - title = self._form_data.get_str('title') - description = self._form_data.get_str('description') + try: + is_active = self._form_data.get_str('is_active') == 'True' + title = self._form_data.get_str('title') + description = self._form_data.get_str('description') + except NotFoundException as e: + raise BadFormatException(e) from e condition.is_active = is_active condition.title.set(title) condition.description.set(description) diff --git a/tests/misc.py b/tests/misc.py index a27f0d0..e72c6d7 100644 --- a/tests/misc.py +++ b/tests/misc.py @@ -2,7 +2,7 @@ from unittest import TestCase from tests.utils import TestCaseWithServer from plomtask.http import InputsParser -from plomtask.exceptions import BadFormatException +from plomtask.exceptions import BadFormatException, NotFoundException class TestsSansServer(TestCase): @@ -14,13 +14,13 @@ class TestsSansServer(TestCase): self.assertEqual('', parser.get_str('foo')) self.assertEqual('bar', parser.get_str('foo', 'bar')) parser.strict = True - with self.assertRaises(BadFormatException): + with self.assertRaises(NotFoundException): parser.get_str('foo') - with self.assertRaises(BadFormatException): + with self.assertRaises(NotFoundException): parser.get_str('foo', 'bar') parser = InputsParser({'foo': []}, False) self.assertEqual('bar', parser.get_str('foo', 'bar')) - with self.assertRaises(BadFormatException): + with self.assertRaises(NotFoundException): InputsParser({'foo': []}, True).get_str('foo', 'bar') for strictness in (False, True): parser = InputsParser({'foo': ['baz']}, strictness) @@ -99,10 +99,6 @@ class TestsSansServer(TestCase): def test_InputsParser_get_float(self) -> None: """Test InputsParser.get_float on strict and non-strict.""" for strictness in (False, True): - with self.assertRaises(BadFormatException): - InputsParser({}, strictness).get_float('foo') - with self.assertRaises(BadFormatException): - InputsParser({'foo': []}, strictness).get_float('foo') with self.assertRaises(BadFormatException): InputsParser({'foo': ['']}, strictness).get_float('foo') with self.assertRaises(BadFormatException): @@ -113,6 +109,16 @@ class TestsSansServer(TestCase): self.assertEqual(0.1, parser.get_float('foo')) parser = InputsParser({'foo': ['1.23', '456']}, strictness) self.assertEqual(1.23, parser.get_float('foo')) + if strictness: + with self.assertRaises(NotFoundException): + InputsParser({}, strictness).get_float('foo') + with self.assertRaises(NotFoundException): + InputsParser({'foo': []}, strictness).get_float('foo') + else: + with self.assertRaises(BadFormatException): + InputsParser({}, strictness).get_float('foo') + with self.assertRaises(BadFormatException): + InputsParser({'foo': []}, strictness).get_float('foo') def test_InputsParser_get_all_str(self) -> None: """Test InputsParser.get_all_str on strict and non-strict."""