Skip to content

Commit bfa00c1

Browse files
authored
Merge pull request #51 from TTWShell/enhance/err_handler
Enhance/err handler
2 parents ffb0b9b + 50d0463 commit bfa00c1

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Change history
22
==============
33

4+
1.3.0a4 (2018-11-15)
5+
6+
* Add ErrHandler.handler_assertion_error.
7+
48
1.3.0a3 (2018-11-11)
59

610
* Hobbit startproject cmd support celery option.

hobbit_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = [1, 3, 0, 'a3']
1+
VERSION = [1, 3, 0, 'a4']

hobbit_core/flask_hobbit/err_handler.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,25 @@ def handler_sqlalchemy_orm_exc(cls, e):
3333
code, message, detail = 500, RESP_MSGS[500], repr(e)
3434

3535
if isinstance(e, orm_exc.NoResultFound):
36-
code, message, detail = 404, '源数据未找到', repr(e)
36+
code, message, detail = 404, u'源数据未找到', repr(e)
3737

3838
return Result(gen_response(code, message, detail), status=code)
3939

40+
@classmethod
41+
def handler_assertion_error(cls, e):
42+
code, message, detail = 422, str(e), repr(e)
43+
return Result(gen_response(code, message, detail), status=code)
44+
4045
@classmethod
4146
def handler_others(cls, e):
4247
traceback.print_exc()
43-
return ServerErrorResult(500, detail=repr(e))
48+
return ServerErrorResult(code=500, detail=repr(e))
4449

4550
@classmethod
4651
def handler(cls, e):
47-
exc = 'others' if not hasattr(e, '__module__') else \
48-
e.__module__.replace('.', '_')
52+
exc = 'others'
53+
if hasattr(e, '__module__'):
54+
exc = e.__module__.replace('.', '_')
55+
elif isinstance(e, AssertionError):
56+
exc = 'assertion_error'
4957
return getattr(cls, 'handler_{}'.format(exc), cls.handler_others)(e)

tests/test_err_handler.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- encoding: utf-8 -*-
2+
import json
3+
4+
from sqlalchemy.orm import exc as orm_exc
5+
from werkzeug import exceptions as wkz_exc
6+
7+
from hobbit_core.flask_hobbit.err_handler import ErrHandler
8+
9+
from . import BaseTest
10+
11+
12+
class TestErrHandler(BaseTest):
13+
14+
def test_assertion_error(self):
15+
resp = ErrHandler.handler(AssertionError('message'))
16+
assert resp.status_code == 422
17+
data = json.loads(resp.get_data())
18+
assert data['message'] == 'message'
19+
20+
def test_sqlalchemy_orm_exc(self):
21+
resp = ErrHandler.handler(orm_exc.NoResultFound())
22+
assert resp.status_code == 404
23+
data = json.loads(resp.get_data())
24+
assert data['message'] == u'源数据未找到'
25+
26+
resp = ErrHandler.handler(orm_exc.UnmappedError())
27+
assert resp.status_code == 500
28+
data = json.loads(resp.get_data())
29+
assert data['message'] == u'服务器内部错误'
30+
31+
def test_werkzeug_exceptions(self):
32+
resp = ErrHandler.handler(wkz_exc.Unauthorized())
33+
assert resp.status_code == 401
34+
data = json.loads(resp.get_data())
35+
assert data['message'] == u'未登录'
36+
37+
def test_others(self):
38+
resp = ErrHandler.handler(Exception('msg'))
39+
assert resp.status_code == 500
40+
data = json.loads(resp.get_data())
41+
assert data['message'] == u'服务器内部错误'
42+
# py27,py36 == "Exception('msg',)"
43+
# py37 == "Exception('msg')"
44+
assert data['detail'].startswith("Exception('msg'")

0 commit comments

Comments
 (0)