Skip to content

Commit fdea637

Browse files
authored
Merge pull request #479 from weakish/engine-error
fix(engine): LeanEngineError status
2 parents d2a2107 + cdf4fc1 commit fdea637

6 files changed

Lines changed: 55 additions & 11 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ pip install -e .'[test]'
5050
Run tests:
5151

5252
```sh
53-
nosetests
53+
python -m nose
54+
```
55+
56+
Run a single test:
57+
58+
```sh
59+
python -m nose tests/test_engine.py:test_lean_engine_error
5460
```
5561

5662
## Linter and Formatter

leancloud/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .errors import LeanCloudWarning
3030
from .file_ import File
3131
from .geo_point import GeoPoint
32-
from .message import Message # noqa: F401
32+
from .message import Message # noqa: F401
3333
from .object_ import Object
3434
from .push import Installation
3535
from .query import FriendshipQuery

leancloud/engine/leanengine.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,31 @@
3030

3131

3232
class LeanEngineError(Exception):
33-
def __init__(self, code=400, message="error"):
34-
if isinstance(code, six.string_types):
35-
message = code
36-
code = 400
37-
self.code = code
38-
self.message = message
33+
def __init__(
34+
self,
35+
code=None, # for backward compatibility, should be 400
36+
message="error", # for backward compatibility, should be required
37+
status=None, # for backward compatibility, should be 400
38+
):
39+
if status is None:
40+
if isinstance(code, six.string_types):
41+
self.message = code
42+
self.code = 400 # for backward compatibility, should be 1
43+
self.status = 400
44+
else:
45+
self.message = message
46+
# for backward compatibility, should be 1
47+
self.code = 400 if code is None else code
48+
self.status = self.code
49+
else:
50+
if isinstance(code, six.string_types):
51+
self.message = code
52+
self.code = 400
53+
self.status = status
54+
else:
55+
self.status = status
56+
self.code = 1 if code is None else code
57+
self.message = message
3958

4059

4160
class LeanEngineApplication(object):
@@ -186,7 +205,7 @@ def dispatch_request(self, environ):
186205
except LeanEngineError as e:
187206
return Response(
188207
json.dumps({"code": e.code, "error": e.message}), # noqa: B306
189-
status=e.code if e.code else 400,
208+
status=e.status,
190209
mimetype="application/json",
191210
)
192211
except Exception:

leancloud/query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CQLResult(object):
3030
3131
class_name: 查询的 class 名称
3232
"""
33+
3334
__slots__ = ["results", "count", "class_name"]
3435

3536
def __init__(self, results, count, class_name):

leancloud/role.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ def validate(self, attrs):
6868
raise TypeError("role name must be string_types")
6969
r = re.compile(r"^[0-9a-zA-Z\-_]+$")
7070
if not r.match(new_name):
71-
raise TypeError("""
71+
raise TypeError(
72+
"""
7273
role's name can only contain alphanumeric characters, _, -, and spaces.
73-
""")
74+
"""
75+
)
7476

7577
return super(Role, self).validate(attrs)

tests/test_engine.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,27 @@ def test_ping(): # type: () -> None
9393

9494

9595
def test_lean_engine_error():
96+
err = leancloud.LeanEngineError(status=404, code=1234567, message="nowhere")
97+
assert err.status == 404
98+
assert err.code == 1234567
99+
assert err.message == "nowhere"
100+
# backward compatibility tests
101+
err = leancloud.LeanEngineError(code=2020, message="eanCloud")
102+
assert err.status == 2020
103+
assert err.code == 2020
104+
assert err.message == "eanCloud"
96105
err = leancloud.LeanEngineError(233, "llllleancloud")
106+
assert err.status == 233
97107
assert err.code == 233
98108
assert err.message == "llllleancloud"
99109
err = leancloud.LeanEngineError("error messages")
110+
assert err.status == 400
100111
assert err.code == 400
101112
assert err.message == "error messages"
113+
err = leancloud.LeanEngineError()
114+
assert err.status == 400
115+
assert err.code == 400
116+
assert err.message == "error"
102117

103118

104119
def test_origin_response(): # type: () -> None
@@ -309,6 +324,7 @@ def _messageReceived():
309324
try:
310325
cloud.run.local("_messageReceived")
311326
except leancloud.LeanEngineError as e:
327+
assert_equal(e.status, 401)
312328
assert_equal(e.code, 401)
313329
else:
314330
raise AssertionError

0 commit comments

Comments
 (0)