I have some simple code to raise an error in 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()
And some classes
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
}
}
Within my local environment it returns a 500 error like this:
{
"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"
}
But within Openshift it complains about:
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
Has anyone experienced this or know why there is a difference between environments?
When trying to trace the error on werkzeug/exceptions.py:95
on the github repo [1], you'll see that the causing LOC is actually not line 95, but rather line 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]
Which should be:
@property
def name(self):
"""The status name."""
return HTTP_STATUS_CODES.get(self.code, 'Unknown Error')
The logical conclusion is that the versions installed on Openshift and on your local machine differ, as you probably don't have Werkzeug explicitly defined on your requirements.txt
.
I replicated this issue by creating an app, and on my machine:
$ pip freeze
Werkzeug==0.11.4
On Openshift:
$ pip freeze
Werkzeug==0.8.3
So, simply add Werkzeug==0.11.4
and you should be good to go.