2121 */
2222package com .influxdb .exceptions ;
2323
24+ import java .util .Collections ;
25+ import java .util .HashMap ;
2426import java .util .Map ;
2527import java .util .function .Predicate ;
2628import javax .annotation .Nonnull ;
2729import javax .annotation .Nullable ;
2830
29- import com .google .gson .JsonObject ;
3031import okhttp3 .MediaType ;
3132import okhttp3 .Protocol ;
3233import 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