Skip to content

Commit b2e51db

Browse files
deserializing unknown key error response (#1041) (#1042)
Co-authored-by: Laura Trotta <[email protected]>
1 parent a169537 commit b2e51db

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

java-client/src/main/java/co/elastic/clients/transport/endpoints/EndpointBase.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import co.elastic.clients.elasticsearch._types.ErrorCause;
2323
import co.elastic.clients.elasticsearch._types.ErrorResponse;
24+
import co.elastic.clients.json.JsonData;
2425
import co.elastic.clients.json.JsonpDeserializer;
2526
import co.elastic.clients.json.JsonpDeserializerBase;
2627
import co.elastic.clients.json.JsonpMapper;
@@ -145,28 +146,43 @@ public JsonpDeserializer<ErrorResponse> errorDeserializer(int statusCode) {
145146
@Override
146147
public ErrorResponse deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
147148
ErrorResponse.Builder builder = new ErrorResponse.Builder();
149+
ErrorCause.Builder errorCauseBuilder = new ErrorCause.Builder();
150+
ErrorCause errorCause = null;
148151
builder.status(statusCode);
149152
while ((event = parser.next()) != JsonParser.Event.END_OBJECT) {
150153
JsonpUtils.expectEvent(parser, JsonParser.Event.KEY_NAME, event);
151154
switch (parser.getString()) {
152155
case "error":
153156
switch (event = parser.next()) {
154157
case VALUE_STRING:
155-
builder.error(e -> e.reason(parser.getString()).type("http_status_" + statusCode));
158+
errorCauseBuilder.reason(parser.getString()).type("http_status_" + statusCode);
156159
break;
157160
default:
158161
JsonpUtils.expectEvent(parser, JsonParser.Event.START_OBJECT, event);
159-
builder.error(ErrorCause._DESERIALIZER.deserialize(parser, mapper, event));
162+
errorCause = ErrorCause._DESERIALIZER.deserialize(parser, mapper, event);
160163
break;
161164
}
162165
break;
163166
case "status":
164167
JsonpUtils.expectNextEvent(parser, JsonParser.Event.VALUE_NUMBER);
165168
builder.status(parser.getInt());
166169
break;
170+
// could be additional information, deserializing it into error cause's metadata
171+
// for example 404 on GetAliasRequest
172+
default:
173+
String key = parser.getString();
174+
event = parser.next();
175+
JsonpUtils.expectEvent(parser, JsonParser.Event.START_OBJECT, event);
176+
errorCauseBuilder.metadata(key, JsonData._DESERIALIZER.deserialize(parser, mapper));
177+
break;
167178
}
168179
}
169-
180+
if (errorCause != null) {
181+
builder.error(errorCause);
182+
}
183+
else {
184+
builder.error(errorCauseBuilder.build());
185+
}
170186
return builder.build();
171187
}
172188
};

0 commit comments

Comments
 (0)