diff --git a/docs/changelog.rst b/docs/changelog.rst index 8709df6..0c048dc 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,10 @@ Change history ============== +1.3.0a4 (2018-11-15) + +* Add ErrHandler.handler_assertion_error. + 1.3.0a3 (2018-11-11) * Hobbit startproject cmd support celery option. diff --git a/hobbit_core/__init__.py b/hobbit_core/__init__.py index 6234ae9..1d4711a 100644 --- a/hobbit_core/__init__.py +++ b/hobbit_core/__init__.py @@ -1 +1 @@ -VERSION = [1, 3, 0, 'a3'] +VERSION = [1, 3, 0, 'a4'] diff --git a/hobbit_core/flask_hobbit/err_handler.py b/hobbit_core/flask_hobbit/err_handler.py index 83dd212..6513fc9 100644 --- a/hobbit_core/flask_hobbit/err_handler.py +++ b/hobbit_core/flask_hobbit/err_handler.py @@ -33,17 +33,25 @@ def handler_sqlalchemy_orm_exc(cls, e): code, message, detail = 500, RESP_MSGS[500], repr(e) if isinstance(e, orm_exc.NoResultFound): - code, message, detail = 404, '源数据未找到', repr(e) + code, message, detail = 404, u'源数据未找到', repr(e) return Result(gen_response(code, message, detail), status=code) + @classmethod + def handler_assertion_error(cls, e): + code, message, detail = 422, str(e), repr(e) + return Result(gen_response(code, message, detail), status=code) + @classmethod def handler_others(cls, e): traceback.print_exc() - return ServerErrorResult(500, detail=repr(e)) + return ServerErrorResult(code=500, detail=repr(e)) @classmethod def handler(cls, e): - exc = 'others' if not hasattr(e, '__module__') else \ - e.__module__.replace('.', '_') + exc = 'others' + if hasattr(e, '__module__'): + exc = e.__module__.replace('.', '_') + elif isinstance(e, AssertionError): + exc = 'assertion_error' return getattr(cls, 'handler_{}'.format(exc), cls.handler_others)(e) diff --git a/tests/test_err_handler.py b/tests/test_err_handler.py new file mode 100644 index 0000000..3e764bd --- /dev/null +++ b/tests/test_err_handler.py @@ -0,0 +1,44 @@ +# -*- encoding: utf-8 -*- +import json + +from sqlalchemy.orm import exc as orm_exc +from werkzeug import exceptions as wkz_exc + +from hobbit_core.flask_hobbit.err_handler import ErrHandler + +from . import BaseTest + + +class TestErrHandler(BaseTest): + + def test_assertion_error(self): + resp = ErrHandler.handler(AssertionError('message')) + assert resp.status_code == 422 + data = json.loads(resp.get_data()) + assert data['message'] == 'message' + + def test_sqlalchemy_orm_exc(self): + resp = ErrHandler.handler(orm_exc.NoResultFound()) + assert resp.status_code == 404 + data = json.loads(resp.get_data()) + assert data['message'] == u'源数据未找到' + + resp = ErrHandler.handler(orm_exc.UnmappedError()) + assert resp.status_code == 500 + data = json.loads(resp.get_data()) + assert data['message'] == u'服务器内部错误' + + def test_werkzeug_exceptions(self): + resp = ErrHandler.handler(wkz_exc.Unauthorized()) + assert resp.status_code == 401 + data = json.loads(resp.get_data()) + assert data['message'] == u'未登录' + + def test_others(self): + resp = ErrHandler.handler(Exception('msg')) + assert resp.status_code == 500 + data = json.loads(resp.get_data()) + assert data['message'] == u'服务器内部错误' + # py27,py36 == "Exception('msg',)" + # py37 == "Exception('msg')" + assert data['detail'].startswith("Exception('msg'")