Skip to content

Commit 2194288

Browse files
authored
feat: gets HTTP headers from the unsuccessful response (#317)
1 parent aa30362 commit 2194288

3 files changed

Lines changed: 99 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44
1. [#316](https://github.com/influxdata/influxdb-client-java/pull/316): Add `InvocableScriptsApi` to create, update, list, delete and invoke scripts by seamless way
55
1. [#315](https://github.com/influxdata/influxdb-client-java/pull/315): Add support for timezones [FluxDSL]
6+
1. [#317](https://github.com/influxdata/influxdb-client-java/pull/317): Gets HTTP headers from the unsuccessful HTTP request
67

78
### Bug Fixes
89
1. [#313](https://github.com/influxdata/influxdb-client-java/pull/313): Do not deliver `exception` when the consumer is already disposed [influxdb-client-reactive]

client-core/src/main/java/com/influxdb/exceptions/InfluxException.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@
2121
*/
2222
package com.influxdb.exceptions;
2323

24+
import java.util.Collections;
2425
import java.util.HashMap;
2526
import java.util.Map;
27+
import java.util.TreeMap;
28+
import java.util.function.Supplier;
2629
import java.util.logging.Level;
2730
import java.util.logging.Logger;
31+
import java.util.stream.Collectors;
2832
import java.util.stream.Stream;
33+
import java.util.stream.StreamSupport;
2934
import javax.annotation.Nonnull;
3035
import javax.annotation.Nullable;
3136

3237
import com.google.gson.Gson;
3338
import com.google.gson.reflect.TypeToken;
39+
import kotlin.Pair;
3440
import okhttp3.ResponseBody;
3541
import retrofit2.HttpException;
3642
import retrofit2.Response;
@@ -115,6 +121,27 @@ public int status() {
115121
return 0;
116122
}
117123

124+
/**
125+
* Gets the HTTP headers from the unsuccessful response.
126+
* If the response is not present than return empty {@code Map}.
127+
*
128+
* @return HTTP headers
129+
*/
130+
@Nonnull
131+
public Map<String, String> headers() {
132+
if (response != null) {
133+
return StreamSupport
134+
.stream(response.headers().spliterator(), false)
135+
.collect(Collectors.toMap(
136+
Pair::component1,
137+
Pair::component2,
138+
(oldValue, newValue) -> newValue,
139+
(Supplier<Map<String, String>>) () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER)));
140+
}
141+
142+
return Collections.emptyMap();
143+
}
144+
118145
/**
119146
* The JSON unsuccessful response body.
120147
*

client-core/src/test/java/com/influxdb/exceptions/InfluxExceptionTest.java

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
*/
2222
package com.influxdb.exceptions;
2323

24+
import java.util.Collections;
25+
import java.util.HashMap;
2426
import java.util.Map;
2527
import java.util.function.Predicate;
2628
import javax.annotation.Nonnull;
2729
import javax.annotation.Nullable;
2830

29-
import com.google.gson.JsonObject;
3031
import okhttp3.MediaType;
3132
import okhttp3.Protocol;
3233
import okhttp3.Request;
@@ -173,7 +174,7 @@ void errorBodyInfluxDB() {
173174
})
174175
.matches((Predicate<Throwable>) throwable -> throwable.getMessage().equals("user not found"));
175176
}
176-
177+
177178
@Test
178179
void errorBodyInfluxDBWithoutMsg() {
179180
Assertions
@@ -205,7 +206,7 @@ void errorBodyReadAgain() {
205206
Map errorBody1 = ((InfluxException) throwable).errorBody();
206207
Map errorBody2 = ((InfluxException) throwable).errorBody();
207208
return errorBody1.get("error").equals("error-body")
208-
&& errorBody1.equals(errorBody2);
209+
&& errorBody1.equals(errorBody2);
209210
});
210211
}
211212

@@ -233,8 +234,8 @@ void errorBodyResponseWithoutBody() {
233234
void message() {
234235

235236
Assertions.assertThatThrownBy(() -> {
236-
throw new InfluxException("Wrong query");
237-
})
237+
throw new InfluxException("Wrong query");
238+
})
238239
.isInstanceOf(InfluxException.class)
239240
.hasMessage("Wrong query")
240241
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).status() == 0)
@@ -247,8 +248,8 @@ void message() {
247248
void messageNull() {
248249

249250
Assertions.assertThatThrownBy(() -> {
250-
throw new InfluxException((String) null);
251-
})
251+
throw new InfluxException((String) null);
252+
})
252253
.isInstanceOf(InfluxException.class)
253254
.hasMessage(null)
254255
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).status() == 0)
@@ -257,11 +258,55 @@ void messageNull() {
257258

258259
}
259260

261+
@Test
262+
void headerValues() {
263+
Assertions
264+
.assertThatThrownBy(() -> {
265+
Response<Object> response = errorResponse(
266+
"not found",
267+
404,
268+
15,
269+
"not-json",
270+
"X-Platform-Error-Code",
271+
Collections.singletonMap("Retry-After", "145"));
272+
throw new InfluxException(new HttpException(response));
273+
})
274+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).headers().size() == 3)
275+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).headers().get("Retry-After").equals("145"))
276+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).headers().get("X-Platform-Error-Code").equals("not found"))
277+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).headers().get("X-Influx-Reference").equals("15"));
278+
}
279+
280+
@Test
281+
void headerValuesEmpty() {
282+
Assertions
283+
.assertThatThrownBy(() -> {
284+
throw new InfluxException("Wrong query");
285+
})
286+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).headers().isEmpty());
287+
}
288+
289+
@Test
290+
void headerValuesIgnoreCase() {
291+
Assertions
292+
.assertThatThrownBy(() -> {
293+
Response<Object> response = errorResponse(
294+
"not found",
295+
404,
296+
15,
297+
"not-json",
298+
"X-Platform-Error-Code",
299+
Collections.singletonMap("Retry-After", "145"));
300+
throw new InfluxException(new HttpException(response));
301+
})
302+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).headers().get("retry-after").equals("145"));
303+
}
304+
260305
@Test
261306
void nullResponse() {
262307
Assertions.assertThatThrownBy(() -> {
263-
throw new InfluxException((Response<?>) null);
264-
})
308+
throw new InfluxException((Response<?>) null);
309+
})
265310
.isInstanceOf(InfluxException.class)
266311
.hasMessage(null)
267312
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).status() == 0)
@@ -294,10 +339,21 @@ private Response<Object> errorResponse(@Nullable final String influxError, final
294339
}
295340

296341
@Nonnull
297-
private Response<Object> errorResponse(@Nullable final String influxError, final int responseCode,
342+
private Response<Object> errorResponse(@Nullable final String influxError,
343+
final int responseCode,
298344
@Nullable final Integer referenceCode,
299345
@Nonnull final String errorBody,
300-
@Nonnull final String headerErrorName) {
346+
@Nonnull final String errorHeaderName) {
347+
return errorResponse(influxError, responseCode, referenceCode, errorBody, errorHeaderName, new HashMap<>());
348+
}
349+
350+
@Nonnull
351+
private Response<Object> errorResponse(@Nullable final String influxError,
352+
final int responseCode,
353+
@Nullable final Integer referenceCode,
354+
@Nonnull final String errorBody,
355+
@Nonnull final String errorHeaderName,
356+
@Nonnull final Map<String, String> headers) {
301357

302358
okhttp3.Response.Builder builder = new okhttp3.Response.Builder() //
303359
.code(responseCode)
@@ -306,14 +362,16 @@ private Response<Object> errorResponse(@Nullable final String influxError, final
306362
.request(new Request.Builder().url("http://localhost/").build());
307363

308364
if (influxError != null) {
309-
builder.addHeader(headerErrorName, influxError);
365+
builder.addHeader(errorHeaderName, influxError);
310366
}
311367

312368
if (referenceCode != null) {
313369
builder.addHeader("X-Influx-Reference", referenceCode.toString());
314370
}
315371

316-
ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), errorBody);
372+
headers.forEach(builder::addHeader);
373+
374+
ResponseBody body = ResponseBody.create(errorBody, MediaType.parse("application/json"));
317375

318376
return Response.error(body, builder.build());
319377
}

0 commit comments

Comments
 (0)