jsonresponse: add x-error-code header in case of error (#89784)
gitea/passerelle/pipeline/head This commit looks good
Details
gitea/passerelle/pipeline/head This commit looks good
Details
This commit is contained in:
parent
c25a371c10
commit
f261ea8f8e
|
@ -91,9 +91,10 @@ class to_json:
|
|||
}
|
||||
if getattr(err, 'extra_dict', None):
|
||||
response.update(err.extra_dict)
|
||||
|
||||
return response
|
||||
|
||||
def render_data(self, req, data, status=200):
|
||||
def render_data(self, req, data, status=200, headers=None):
|
||||
debug = DEFAULT_DEBUG
|
||||
debug = debug or req.GET.get('debug', 'false').lower() in ('true', 't', '1', 'on')
|
||||
debug = debug or req.GET.get('decode', '0').lower() in ('true', 't', '1', 'on')
|
||||
|
@ -116,7 +117,9 @@ class to_json:
|
|||
plain = '%s(%s);' % (jsonp_cb, plain)
|
||||
content_type = 'application/javascript'
|
||||
|
||||
return HttpResponse(plain, content_type='%s; charset=UTF-8' % content_type, status=status)
|
||||
return HttpResponse(
|
||||
plain, content_type='%s; charset=UTF-8' % content_type, status=status, headers=headers
|
||||
)
|
||||
|
||||
def api_func(self, f, *args, **kwargs):
|
||||
return self.api(f, args[0], *args, **kwargs)
|
||||
|
@ -176,6 +179,7 @@ class to_json:
|
|||
data = self.err_to_response(e)
|
||||
if getattr(e, 'err_code', None):
|
||||
data['err'] = e.err_code
|
||||
headers = {'x-error-code': data['err']}
|
||||
if getattr(e, 'http_status', None):
|
||||
status = e.http_status
|
||||
elif isinstance(e, (ObjectDoesNotExist, Http404)):
|
||||
|
@ -185,7 +189,7 @@ class to_json:
|
|||
else:
|
||||
status = self.error_code
|
||||
|
||||
response = self.render_data(req, data, status)
|
||||
response = self.render_data(req, data, status, headers)
|
||||
|
||||
# We preempt Django logging of HTTP responses to prevent it from
|
||||
# logging our 500 responses as errors.
|
||||
|
|
Loading…
Reference in New Issue