Skip to content

Commit a243a77

Browse files
authored
Merge pull request #480 from weakish/master
fix: LeanEngineError return valid http status
2 parents fdea637 + c799f17 commit a243a77

2 files changed

Lines changed: 72 additions & 5 deletions

File tree

leancloud/engine/leanengine.py

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,69 @@
2828
user = context.local("user")
2929
current = context.local("current")
3030

31+
# http.HTTPStatus is not available in Python 2
32+
http_status_codes = {
33+
100,
34+
101,
35+
102,
36+
200,
37+
201,
38+
202,
39+
203,
40+
204,
41+
205,
42+
206,
43+
207,
44+
208,
45+
226,
46+
300,
47+
301,
48+
302,
49+
303,
50+
304,
51+
305,
52+
307,
53+
308,
54+
400,
55+
401,
56+
402,
57+
403,
58+
404,
59+
405,
60+
406,
61+
407,
62+
408,
63+
409,
64+
410,
65+
411,
66+
412,
67+
413,
68+
414,
69+
415,
70+
416,
71+
417,
72+
421,
73+
422,
74+
423,
75+
424,
76+
426,
77+
428,
78+
429,
79+
431,
80+
451,
81+
500,
82+
501,
83+
502,
84+
503,
85+
504,
86+
505,
87+
506,
88+
507,
89+
508,
90+
510,
91+
511,
92+
}
93+
3194

3295
class LeanEngineError(Exception):
3396
def __init__(
@@ -45,16 +108,16 @@ def __init__(
45108
self.message = message
46109
# for backward compatibility, should be 1
47110
self.code = 400 if code is None else code
48-
self.status = self.code
111+
self.status = self.code if self.code in http_status_codes else 400
49112
else:
50113
if isinstance(code, six.string_types):
51114
self.message = code
52115
self.code = 400
53116
self.status = status
54117
else:
55-
self.status = status
56-
self.code = 1 if code is None else code
57118
self.message = message
119+
self.code = 1 if code is None else code
120+
self.status = status
58121

59122

60123
class LeanEngineApplication(object):

tests/test_engine.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,17 @@ def test_lean_engine_error():
9999
assert err.message == "nowhere"
100100
# backward compatibility tests
101101
err = leancloud.LeanEngineError(code=2020, message="eanCloud")
102-
assert err.status == 2020
102+
assert err.status == 400
103103
assert err.code == 2020
104104
assert err.message == "eanCloud"
105105
err = leancloud.LeanEngineError(233, "llllleancloud")
106-
assert err.status == 233
106+
assert err.status == 400
107107
assert err.code == 233
108108
assert err.message == "llllleancloud"
109+
err = leancloud.LeanEngineError(226, "leancloud")
110+
assert err.status == 226
111+
assert err.code == 226
112+
assert err.message == "leancloud"
109113
err = leancloud.LeanEngineError("error messages")
110114
assert err.status == 400
111115
assert err.code == 400

0 commit comments

Comments
 (0)