Skip to content

Commit 052f66b

Browse files
pquentingithub-actions[bot]
authored andcommitted
Surface caused_by in ApiError (#2932)
In particular, this will give a clear error when using elasticsearch-py 9.0.0 on an 8.x cluster. (cherry picked from commit 72efd52)
1 parent 572e022 commit 052f66b

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

elasticsearch/exceptions.py

+2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ def __str__(self) -> str:
6161
if self.body and isinstance(self.body, dict) and "error" in self.body:
6262
if isinstance(self.body["error"], dict):
6363
root_cause = self.body["error"]["root_cause"][0]
64+
caused_by = self.body["error"].get("caused_by", {})
6465
cause = ", ".join(
6566
filter(
6667
None,
6768
[
6869
repr(root_cause["reason"]),
6970
root_cause.get("resource.id"),
7071
root_cause.get("resource.type"),
72+
caused_by.get("reason"),
7173
],
7274
)
7375
)

test_elasticsearch/test_exceptions.py

+30
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,33 @@ def test_transform_error_parse_with_error_string(self):
4646
assert (
4747
str(e) == "ApiError(500, 'InternalServerError', 'something error message')"
4848
)
49+
50+
def test_transform_invalid_media_type_error(self):
51+
e = ApiError(
52+
message="InvalidMediaType",
53+
meta=error_meta,
54+
body={
55+
"error": {
56+
"root_cause": [
57+
{
58+
"type": "media_type_header_exception",
59+
"reason": "Invalid media-type value on headers [Accept, Content-Type]",
60+
}
61+
],
62+
"type": "media_type_header_exception",
63+
"reason": "Invalid media-type value on headers [Accept, Content-Type]",
64+
"caused_by": {
65+
"type": "status_exception",
66+
"reason": "Accept version must be either version 8 or 7, but found 9. Accept=application/vnd.elasticsearch+json; compatible-with=9",
67+
},
68+
},
69+
"status": 400,
70+
},
71+
)
72+
73+
assert str(e) == (
74+
"ApiError(500, 'InvalidMediaType', "
75+
"'Invalid media-type value on headers [Accept, Content-Type]', "
76+
"Accept version must be either version 8 or 7, but found 9. "
77+
"Accept=application/vnd.elasticsearch+json; compatible-with=9)"
78+
)

0 commit comments

Comments
 (0)