【问题标题】:Python Rest Http Error (Flask) with Openshift使用 Openshift 的 Python Rest Http 错误(Flask)
【发布时间】:2016-05-18 23:48:26
【问题描述】:

我有一些简单的代码可以在 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

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

【问题讨论】:

  • 您能否在您的custom_errors dict 中添加一个None 键值以至少可以访问请求信息?

标签: python error-handling flask openshift flask-restful


【解决方案1】:

当尝试在 github repo [1] 上跟踪 werkzeug/exceptions.py:95 上的错误时,您会看到导致 LOC 实际上不是第 95 行,而是第 109 行 [2]

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 就可以了。

【讨论】:

  • 先生,您是明星。太棒了!
  • 我永远不会找到这个。感谢分享!
猜你喜欢
  • 2015-02-24
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 2014-07-07
  • 2012-12-30
  • 2018-07-04
  • 1970-01-01
相关资源
最近更新 更多