Skip to content
Snippets Groups Projects
Unverified Commit 1505e40c authored by Harsha Kethineni's avatar Harsha Kethineni Committed by GitHub
Browse files

Merge pull request #1002 from scitran/err-request-id

Error response contains request id
parents bbcbe7d9 990bec8f
No related branches found
No related tags found
No related merge requests found
......@@ -204,19 +204,20 @@ def format_hash(hash_alg, hash_):
return '-'.join(('v0', hash_alg, hash_))
def create_json_http_exception_response(message, code, custom=None):
def create_json_http_exception_response(message, code, request_id, custom=None):
content = {
'message': message,
'status_code': code
'status_code': code,
'request_id': request_id
}
if custom:
content.update(custom)
return content
def send_json_http_exception(response, message, code, custom=None):
def send_json_http_exception(response, message, code, request_id, custom=None):
response.set_status(code)
json_content = json.dumps(create_json_http_exception_response(message, code, custom))
json_content = json.dumps(create_json_http_exception_response(message, code, request_id, custom))
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.write(json_content)
......
......@@ -328,6 +328,7 @@ class RequestHandler(webapp2.RequestHandler):
For all others use a generic 500 error code and log the stack trace
"""
request_id = self.request.id
custom_errors = None
message = str(exception)
if isinstance(exception, webapp2.HTTPException):
......@@ -369,9 +370,9 @@ class RequestHandler(webapp2.RequestHandler):
self.request.logger.error(tb)
if return_json:
return util.create_json_http_exception_response(message, code, custom=custom_errors)
return util.create_json_http_exception_response(message, code, request_id, custom=custom_errors)
util.send_json_http_exception(self.response, message, code, custom=custom_errors)
util.send_json_http_exception(self.response, message, code, request_id, custom=custom_errors)
def log_user_access(self, access_type, cont_name=None, cont_id=None, multifile=False):
......
......@@ -50,14 +50,14 @@ def dispatcher(router, request, response):
response.write(json.dumps(rv, default=encoder.custom_json_serializer))
response.headers['Content-Type'] = 'application/json; charset=utf-8'
except webapp2.HTTPException as e:
util.send_json_http_exception(response, str(e), e.code)
util.send_json_http_exception(response, str(e), e.code, request.id)
except Exception as e: # pylint: disable=broad-except
request.logger.error("Error dispatching request", exc_info=True)
if config.get_item('core', 'debug'):
message = traceback.format_exc()
else:
message = 'Internal Server Error'
util.send_json_http_exception(response, message, 500)
util.send_json_http_exception(response, message, 500, request.id)
def app_factory(*_, **__):
# pylint: disable=protected-access,unused-argument
......
......@@ -12,3 +12,23 @@ def test_extra_param(as_admin):
r = as_admin.get('/projects')
assert r.ok
assert not any(project['label'] == label for project in r.json())
def test_error_response(as_admin, as_user, as_public, data_builder):
group = data_builder.create_group()
project = data_builder.create_project()
# Test dao exception
r = as_admin.post('/users', json={'_id': "admin@user.com", 'firstname': "Firstname", 'lastname': "Lastname"})
assert r.status_code == 409
assert r.json().get('request_id')
# Test schema exceptions
r = as_admin.post('/groups', json={'foo':'bar'})
assert r.status_code == 400
assert r.json().get('request_id')
# Test Permission exception
r = as_user.put('/projects/' + project, json={'label':'Project'})
assert r.status_code == 403
assert r.json().get('request_id')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment