Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion hobbit_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = [1, 3, 0, 'a3']
VERSION = [1, 3, 0, 'a4']
16 changes: 12 additions & 4 deletions hobbit_core/flask_hobbit/err_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
44 changes: 44 additions & 0 deletions tests/test_err_handler.py
Original file line number Diff line number Diff line change
@@ -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'")