Openshift出现Python Rest Http错误(Flask)

Openshift出现Python Rest Http错误(Flask),python,error-handling,flask,openshift,flask-restful,Python,Error Handling,Flask,Openshift,Flask Restful,我有一些简单的代码来引发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.p

我有一些简单的代码来引发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
    }
}
在我的本地环境中,它返回一个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 repo上的
werkzeug/exceptions.py:95
上跟踪错误时,您会看到导致错误的LOC实际上不是第95行,而是第109行

应该是:

@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
,您就可以开始了。

您可以在
自定义错误中添加
None
键控值,以至少访问请求信息吗?先生,您是明星。太神了我永远也找不到这个。谢谢分享!
$ pip freeze
Werkzeug==0.8.3