Skip to content

Commit 99ae47d

Browse files
committed
Added instructions append for error handling and updated JinJa2 template and regenerated test file.
1 parent 3e40a4f commit 99ae47d

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Instructions append
2+
3+
## Exception messages
4+
5+
Sometimes it is necessary to [raise an exception](https://docs.python.org/3/tutorial/errors.html#raising-exceptions). When you do this, you should always include a **meaningful error message** to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the [built in error types](https://docs.python.org/3/library/exceptions.html#base-classes), but should still include a meaningful message.
6+
7+
This particular exercise requires that you use the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) to "throw" a `ValueError` if the sequence to decode or encode is incomplete. The tests will only pass if you both `raise` the `exception` and include a message with it.
8+
9+
To raise a `ValueError` with a message, write the message as an argument to the `exception` type:
10+
11+
```python
12+
# if the sequence is incomplete
13+
raise ValueError("incomplete sequence")
14+
```

exercises/practice/variable-length-quantity/.meta/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ def decode(bytes_):
2929
values.append(n)
3030
n = 0
3131
elif i == len(bytes_) - 1:
32-
raise ValueError('incomplete byte sequence')
32+
raise ValueError("incomplete sequence")
3333

3434
return values

exercises/practice/variable-length-quantity/.meta/template.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
1515
{%- for sub_case in case.cases %}
1616
def test_{{ sub_case["description"] | to_snake }}(self):
1717
{%- if sub_case is error_case %}
18-
with self.assertRaisesWithMessage(ValueError):
18+
with self.assertRaises(ValueError) as err:
1919
{{ sub_case["property"] }}({{ list_int_to_hex(sub_case["input"]["integers"]) }})
20+
self.assertEqual(type(err.exception), ValueError)
21+
self.assertEqual(err.exception.args[0], "{{ sub_case["expected"]["error"] }}")
2022
{%- else %}
2123
self.assertEqual({{ sub_case["property"] }}({{ list_int_to_hex(sub_case["input"]["integers"]) }}), {{ list_int_to_hex(sub_case["expected"]) }})
2224
{%- endif %}
2325
{% endfor -%}
2426

2527
{% endfor %}
26-
27-
{{ macros.footer() }}

exercises/practice/variable-length-quantity/variable_length_quantity_test.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,16 @@ def test_maximum_32_bit_integer(self):
100100
self.assertEqual(decode([0x8F, 0xFF, 0xFF, 0xFF, 0x7F]), [0xFFFFFFFF])
101101

102102
def test_incomplete_sequence_causes_error(self):
103-
with self.assertRaisesWithMessage(ValueError):
103+
with self.assertRaises(ValueError) as err:
104104
decode([0xFF])
105+
self.assertEqual(type(err.exception), ValueError)
106+
self.assertEqual(err.exception.args[0], "incomplete sequence")
105107

106108
def test_incomplete_sequence_causes_error_even_if_value_is_zero(self):
107-
with self.assertRaisesWithMessage(ValueError):
109+
with self.assertRaises(ValueError) as err:
108110
decode([0x80])
111+
self.assertEqual(type(err.exception), ValueError)
112+
self.assertEqual(err.exception.args[0], "incomplete sequence")
109113

110114
def test_multiple_values(self):
111115
self.assertEqual(
@@ -130,11 +134,3 @@ def test_multiple_values(self):
130134
),
131135
[0x2000, 0x123456, 0xFFFFFFF, 0x0, 0x3FFF, 0x4000],
132136
)
133-
134-
# Utility functions
135-
def assertRaisesWithMessage(self, exception):
136-
return self.assertRaisesRegex(exception, r".+")
137-
138-
139-
if __name__ == "__main__":
140-
unittest.main()

0 commit comments

Comments
 (0)