Skip to content

Commit 5978b63

Browse files
committed
feat: set x-request-id as error trace
1 parent 7d820bc commit 5978b63

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ibmcloudant/cloudant_base_service.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ def _error_response_hook(response:Response, *args, **kwargs) -> Optional[Respons
160160
error_json['errors'] = [error_model]
161161
send_augmented_response = True
162162
if 'errors' in error_json:
163-
trace = response.headers.get('x-couch-request-id')
163+
# Get the x-request-id header if available
164+
# otherwise try the x-couch-request-id header
165+
trace = response.headers.get('x-request-id',
166+
response.headers.get('x-couch-request-id'))
164167
if trace is not None:
165168
# Augment trace if there was a value
166169
error_json['trace'] = trace

test/unit/test_cloudant_base_error_augment.py

+25
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,31 @@ def test_augment_error_reason_with_trace(self):
231231
}
232232
)
233233

234+
def test_augment_error_reason_with_trace_request_id(self):
235+
self._run_test(
236+
mock_response={
237+
'body': self._error_reason_body,
238+
'headers': self._content_type_header | {'x-request-id': self._req_id},
239+
'status': 444
240+
},
241+
expected_response={
242+
'body': self._error_reason_body | self._errors_error_reason | self._trace
243+
}
244+
)
245+
246+
def test_augment_error_reason_with_trace_request_id_preferred(self):
247+
expected_req_id = 'preferred_req_id'
248+
self._run_test(
249+
mock_response={
250+
'body': self._error_reason_body,
251+
'headers': self._default_mock_headers | {'x-request-id': expected_req_id},
252+
'status': 444
253+
},
254+
expected_response={
255+
'body': self._error_reason_body | self._errors_error_reason | {'trace': expected_req_id}
256+
}
257+
)
258+
234259
def test_augment_error_reason_stream(self):
235260
self._run_test(
236261
mock_response={

0 commit comments

Comments
 (0)