Skip to content

Commit

Permalink
Merge pull request #480 from weakish/master
Browse files Browse the repository at this point in the history
fix:  LeanEngineError return valid http status
  • Loading branch information
weakish authored Jun 18, 2020
2 parents fdea637 + c799f17 commit a243a77
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
69 changes: 66 additions & 3 deletions leancloud/engine/leanengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,69 @@
user = context.local("user")
current = context.local("current")

# http.HTTPStatus is not available in Python 2
http_status_codes = {
100,
101,
102,
200,
201,
202,
203,
204,
205,
206,
207,
208,
226,
300,
301,
302,
303,
304,
305,
307,
308,
400,
401,
402,
403,
404,
405,
406,
407,
408,
409,
410,
411,
412,
413,
414,
415,
416,
417,
421,
422,
423,
424,
426,
428,
429,
431,
451,
500,
501,
502,
503,
504,
505,
506,
507,
508,
510,
511,
}


class LeanEngineError(Exception):
def __init__(
Expand All @@ -45,16 +108,16 @@ def __init__(
self.message = message
# for backward compatibility, should be 1
self.code = 400 if code is None else code
self.status = self.code
self.status = self.code if self.code in http_status_codes else 400
else:
if isinstance(code, six.string_types):
self.message = code
self.code = 400
self.status = status
else:
self.status = status
self.code = 1 if code is None else code
self.message = message
self.code = 1 if code is None else code
self.status = status


class LeanEngineApplication(object):
Expand Down
8 changes: 6 additions & 2 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ def test_lean_engine_error():
assert err.message == "nowhere"
# backward compatibility tests
err = leancloud.LeanEngineError(code=2020, message="eanCloud")
assert err.status == 2020
assert err.status == 400
assert err.code == 2020
assert err.message == "eanCloud"
err = leancloud.LeanEngineError(233, "llllleancloud")
assert err.status == 233
assert err.status == 400
assert err.code == 233
assert err.message == "llllleancloud"
err = leancloud.LeanEngineError(226, "leancloud")
assert err.status == 226
assert err.code == 226
assert err.message == "leancloud"
err = leancloud.LeanEngineError("error messages")
assert err.status == 400
assert err.code == 400
Expand Down

0 comments on commit a243a77

Please sign in to comment.