exception-tracker: send exc id headers on failed API calls for tracking errors that server generated.

This commit is contained in:
Marcin Kuzminski 2019-12-02 15:56:54 +01:00
parent 8bfbe9e1dc
commit 0c4dd82cf4
2 changed files with 12 additions and 6 deletions

View file

@ -122,7 +122,7 @@ def jsonrpc_response(request, result):
return response
def jsonrpc_error(request, message, retid=None, code=None):
def jsonrpc_error(request, message, retid=None, code=None, headers=None):
"""
Generate a Response object with a JSON-RPC error body
@ -132,10 +132,12 @@ def jsonrpc_error(request, message, retid=None, code=None):
"""
err_dict = {'id': retid, 'result': None, 'error': message}
body = render(DEFAULT_RENDERER, err_dict, request=request).encode('utf-8')
return Response(
body=body,
status=code,
content_type='application/json'
content_type='application/json',
headerlist=headers
)
@ -287,8 +289,7 @@ def request_view(request):
})
# register some common functions for usage
attach_context_attributes(
TemplateArgs(), request, request.rpc_user.user_id)
attach_context_attributes(TemplateArgs(), request, request.rpc_user.user_id)
try:
ret_value = func(**call_params)
@ -298,9 +299,13 @@ def request_view(request):
except Exception:
log.exception('Unhandled exception occurred on api call: %s', func)
exc_info = sys.exc_info()
store_exception(id(exc_info), exc_info, prefix='rhodecode-api')
exc_id, exc_type_name = store_exception(
id(exc_info), exc_info, prefix='rhodecode-api')
error_headers = [('RhodeCode-Exception-Id', str(exc_id)),
('RhodeCode-Exception-Type', str(exc_type_name))]
return jsonrpc_error(
request, retid=request.rpc_id, message='Internal server error')
request, retid=request.rpc_id, message='Internal server error',
headers=error_headers)
def setup_request(request):

View file

@ -115,6 +115,7 @@ def store_exception(exc_id, exc_info, prefix=global_prefix):
exc_type_name, exc_traceback = _prepare_exception(exc_info)
_store_exception(exc_id=exc_id, exc_type_name=exc_type_name,
exc_traceback=exc_traceback, prefix=prefix)
return exc_id, exc_type_name
except Exception:
log.exception('Failed to store exception `%s` information', exc_id)
# there's no way this can fail, it will crash server badly if it does.