forked from ehsanmok/flare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_errors.mojo
More file actions
244 lines (185 loc) Β· 8.46 KB
/
Copy pathtest_errors.mojo
File metadata and controls
244 lines (185 loc) Β· 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""Tests for :mod:`flare.errors` β the cross-module typed-error
vocabulary.
Coverage:
1. :class:`ValidationError` constructor + field access.
2. :class:`ValidationError.write_to` rendering.
3. :class:`ValidationError` round-trips through a typed-raises
function (``raises ValidationError``) β the caller catches the
typed error and accesses ``field`` / ``reason`` directly per
the Mojo doc Β§ "Catch a typed error".
4. :class:`ValidationError` propagates through bare-``raises``
wrappers with its ``Writable`` rendering preserved (the
"type erasure only affects compile-time type, not runtime
identity" rule from the Mojo doc Β§ "Avoid bare raises with
typed errors").
5. :class:`IoError` constructor + field access + ``write_to``
with and without an ``errno`` value.
6. :class:`IoError` round-trips through a ``raises IoError``
function the same way :class:`ValidationError` does.
"""
from std.testing import (
TestSuite,
assert_equal,
assert_true,
)
from flare.errors import (
HttpStatusError,
IoError,
ValidationError,
http_reason_phrase,
map_handler_error,
parse_status_error,
)
# ββ ValidationError ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_validation_error_construct_and_field_access() raises:
var e = ValidationError(
field=String("username"), reason=String("too short")
)
assert_equal(e.field, String("username"))
assert_equal(e.reason, String("too short"))
def test_validation_error_write_to_renders_field_and_reason() raises:
var e = ValidationError(
field=String("port"), reason=String("must be 1..65535")
)
assert_equal(String(e), String("ValidationError(port): must be 1..65535"))
def _validate_chunk_size(n: Int) raises ValidationError -> Int:
"""Typed-raises helper used by the round-trip tests."""
if n <= 0:
raise ValidationError(
field=String("chunk_size"),
reason=String("must be > 0, got ") + String(n),
)
return n
def test_validation_error_typed_round_trip_preserves_fields() raises:
"""The Mojo doc says ``except e:`` infers the typed error's
type from the function being called β so field access works
without ``String(e)`` heuristics."""
var got_field = String("")
var got_reason = String("")
try:
var _n = _validate_chunk_size(-3)
except e:
got_field = e.field.copy()
got_reason = e.reason.copy()
assert_equal(got_field, String("chunk_size"))
assert_true(got_reason.find("got -3") >= 0)
def _wrap_chunk_size_in_bare_raises(n: Int) raises -> Int:
"""Bare-``raises`` wrapper around a typed-raises function.
The Mojo runtime preserves the typed error's ``Writable``
identity through the bare-raises propagation; only the
compile-time type at the catch site collapses to
``Error``."""
return _validate_chunk_size(n)
def test_validation_error_survives_bare_raises_propagation() raises:
"""The wrapping bare-raises function erases the typed error's
compile-time type, but ``String(e)`` at the catch site still
produces the original typed rendering."""
var msg = String("")
try:
var _n = _wrap_chunk_size_in_bare_raises(0)
except e:
msg = String(e)
assert_true(msg.find("ValidationError(chunk_size)") >= 0)
assert_true(msg.find("must be > 0") >= 0)
# ββ IoError βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_io_error_construct_and_field_access() raises:
var e = IoError(op=String("open"), code=2, detail=String("/etc/secret.txt"))
assert_equal(e.op, String("open"))
assert_equal(e.code, 2)
assert_equal(e.detail, String("/etc/secret.txt"))
def test_io_error_write_to_includes_errno_when_nonzero() raises:
var e = IoError(op=String("read"), code=11, detail=String("would block"))
assert_equal(String(e), String("IoError(read): would block (errno=11)"))
def test_io_error_write_to_omits_errno_when_zero() raises:
"""``code=0`` means "errno is not applicable here" so we
don't render it (would otherwise look like a real errno=0
"no such error" code, which is misleading)."""
var e = IoError(
op=String("alloc"), code=0, detail=String("requested 1 GiB")
)
assert_equal(String(e), String("IoError(alloc): requested 1 GiB"))
def _read_file(path: String) raises IoError -> Int:
"""Typed-raises helper used by the round-trip test."""
if path.byte_length() == 0:
raise IoError(
op=String("open"),
code=22,
detail=String("empty path"),
)
return 0
def test_io_error_typed_round_trip_preserves_fields() raises:
var got_op = String("")
var got_code = -1
try:
var _r = _read_file(String(""))
except e:
got_op = e.op.copy()
got_code = e.code
assert_equal(got_op, String("open"))
assert_equal(got_code, 22)
# ββ HttpStatusError + map_handler_error βββββββββββββββββββββββββββββββββββββ
def test_http_status_error_render() raises:
var e = HttpStatusError(status=404, message=String("user not found"))
assert_equal(String(e), String("HttpStatusError(404): user not found"))
def test_http_status_error_default_message() raises:
var e = HttpStatusError(409)
assert_equal(e.message, String("Conflict"))
assert_equal(String(e), String("HttpStatusError(409): Conflict"))
def test_status_error_codec_round_trip() raises:
"""render -> String -> parse_status_error must reproduce the exact
(status, message) the codec is defined to carry."""
var e = HttpStatusError(status=418, message=String("teapot: 3): x"))
var parsed = parse_status_error(String(e))
assert_true(Bool(parsed))
assert_equal(parsed.value().status, 418)
# Message-containing-"): " must survive (delimiter is the first
# "): " after the numeric status).
assert_equal(parsed.value().reason, String("teapot: 3): x"))
def test_status_error_codec_rejects_non_status() raises:
assert_true(not parse_status_error(String("kaboom")))
assert_true(not parse_status_error(String("HttpStatusError(abc): x")))
assert_true(not parse_status_error(String("HttpStatusError(700): x")))
def _raise_status() raises -> Int:
"""Bare-raises wrapper: erases the type but keeps the rendering."""
raise HttpStatusError(status=403, message=String("nope"))
def test_map_http_status_error_through_bare_raises() raises:
var rendered = String("")
try:
var _r = _raise_status()
except e:
rendered = String(e)
var mapped = map_handler_error(rendered, expose=False)
assert_equal(mapped.status, 403)
# HttpStatusError message is always echoed (handler-authored).
assert_equal(mapped.reason, String("nope"))
def test_map_validation_error_sanitized() raises:
var rendered = String("ValidationError(email): bad <user> input")
var mapped = map_handler_error(rendered, expose=False)
assert_equal(mapped.status, 400)
assert_equal(mapped.reason, String("Bad Request"))
def test_map_validation_error_exposed() raises:
var rendered = String("ValidationError(email): why")
var mapped = map_handler_error(rendered, expose=True)
assert_equal(mapped.status, 400)
assert_equal(mapped.reason, rendered)
def test_map_generic_error_defaults_to_500() raises:
var mapped = map_handler_error(String("kaboom"), expose=False)
assert_equal(mapped.status, 500)
assert_equal(mapped.reason, String("Internal Server Error"))
def test_map_malformed_status_error_falls_back_to_500() raises:
# Looks like the prefix but the code is junk -> safe 500.
var mapped = map_handler_error(
String("HttpStatusError(abc): x"), expose=False
)
assert_equal(mapped.status, 500)
def test_map_out_of_range_status_falls_back_to_500() raises:
var mapped = map_handler_error(
String("HttpStatusError(700): x"), expose=False
)
assert_equal(mapped.status, 500)
def test_reason_phrase_fallbacks() raises:
assert_equal(http_reason_phrase(404), String("Not Found"))
assert_equal(http_reason_phrase(599), String("Server Error"))
assert_equal(http_reason_phrase(450), String("Client Error"))
def main() raises:
TestSuite.discover_tests[__functions_in_module()]().run()