Python Openshift 的 Rest Http 错误(Flask)

Python Rest Http Error (Flask) with Openshift

我有一些简单的代码可以在 Python 中引发错误:

class LoginUser(Resource):
    def post(self):
        # Parse the arguments
        parser = reqparse.RequestParser()
        parser.add_argument('email', type=str, help='Email address to create user')
        args = parser.parse_args()

        _userEmail = args['email']
        _users = User.objects(email=_userEmail)

        if len(_users) == 0:
            raise LoginInvalidEmailError()

还有一些类

from flask_restful import HTTPException

class LoginInvalidEmailError(HTTPException):
    pass

class LoginInvalidPasswordError(HTTPException):
    pass

#http://flask-restful.readthedocs.io/en/latest/extending.html#define-custom-error-messages
custom_errors = {
    'LoginInvalidEmailError': {
        'message': "Email address has not been registered.",
        "code" : 500,
        "status": 500,
        "status_code": 500
    },
    'LoginInvalidPasswordError': {
        'message': "Invalid password.",
        "code" : 500,
        "status": 500,
        "status_code": 500
    }
}

在我的本地环境中,它 returns 500 错误如下:

{
"data": {
    "code": 500,
    "message": "Email address has not been registered.",
    "status": 500,
    "status_code": 500
}, "status": 500, "config": {
    "method": "POST",
    "transformRequest": [null],
    "transformResponse": [null],
    "url": "http://localhost:3030/User/Login",
    "data": {
        "email": "",
        "password": ""
    },
    "headers": {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/json;charset=utf-8",
        "Authorization": "token null",
        "X-XSRF-TOKEN": "62PQ0ri9DC/ZStMLUkR9st7UvOtGVRLs88zoE="
    }
}, "statusText": "INTERNAL SERVER ERROR"
}

但在 Openshift 中它抱怨:

Traceback (most recent call last):
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 271, in error_router
    return original_handler(e)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 268, in error_router
    return self.handle_error(e)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 271, in error_router
    return original_handler(e)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 268, in error_router
    return self.handle_error(e)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 477, in wrapper
    resp = resource(*args, **kwargs)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 587, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/repo/kb/endpoints/users/LoginUser.py", line 19,
    raise LoginInvalidEmailError()
  File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/exceptions.py", line 75, in __init__
    Exception.__init__(self, '%d %s' % (self.code, self.name))
  File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/exceptions.py", line 95, in name
    return HTTP_STATUS_CODES[self.code]
KeyError: None

有没有人遇到过这种情况或知道为什么环境之间存在差异?

尝试在 github 存储库 [1], you'll see that the causing LOC is actually not line 95, but rather line 109 [2]

上跟踪 werkzeug/exceptions.py:95 上的错误时
File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/exceptions.py", line 95, in name
return HTTP_STATUS_CODES[self.code]

应该是:

@property
def name(self):
    """The status name."""
    return HTTP_STATUS_CODES.get(self.code, 'Unknown Error')

合乎逻辑的结论是安装在 Openshift 和本地计算机上的版本不同,因为您可能没有在 requirements.txt.

上明确定义 Werkzeug

我通过创建一个应用程序在我的机器上复制了这个问题:

$ pip freeze
Werkzeug==0.11.4

在 Openshift 上:

$ pip freeze
Werkzeug==0.8.3

所以,只需添加 Werkzeug==0.11.4 就可以了。