File tree 5 files changed +60
-1
lines changed
5 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 1
1
package org .influxdb ;
2
2
3
+ import java .io .InputStream ;
4
+
5
+ import org .msgpack .core .MessagePack ;
6
+ import org .msgpack .core .MessageUnpacker ;
7
+ import org .msgpack .value .ImmutableMapValue ;
8
+ import org .msgpack .value .impl .ImmutableStringValueImpl ;
9
+
3
10
import com .squareup .moshi .JsonAdapter ;
4
11
import com .squareup .moshi .Moshi ;
5
12
@@ -168,4 +175,20 @@ public static InfluxDBException buildExceptionForErrorState(final String errorBo
168
175
return new InfluxDBException (errorBody );
169
176
}
170
177
}
178
+
179
+ /**
180
+ * Create corresponding InfluxDBException from the message pack error body.
181
+ * @param messagePackErrorBody
182
+ * @return
183
+ */
184
+ public static InfluxDBException buildExceptionForErrorState (final InputStream messagePackErrorBody ) {
185
+ try {
186
+ MessageUnpacker unpacker = MessagePack .newDefaultUnpacker (messagePackErrorBody );
187
+ ImmutableMapValue mapVal = (ImmutableMapValue ) unpacker .unpackValue ();
188
+ return InfluxDBException .buildExceptionFromErrorMessage (
189
+ mapVal .map ().get (new ImmutableStringValueImpl ("error" )).toString ());
190
+ } catch (Exception e ) {
191
+ return new InfluxDBException (e );
192
+ }
193
+ }
171
194
}
Original file line number Diff line number Diff line change @@ -696,7 +696,11 @@ private <T> T execute(final Call<T> call) {
696
696
return response .body ();
697
697
}
698
698
try (ResponseBody errorBody = response .errorBody ()) {
699
- throw InfluxDBException .buildExceptionForErrorState (errorBody .string ());
699
+ if (messagePack ) {
700
+ throw InfluxDBException .buildExceptionForErrorState (errorBody .byteStream ());
701
+ } else {
702
+ throw InfluxDBException .buildExceptionForErrorState (errorBody .string ());
703
+ }
700
704
}
701
705
} catch (IOException e ) {
702
706
throw new InfluxDBIOException (e );
Original file line number Diff line number Diff line change
1
+ package org .influxdb ;
2
+
3
+ import org .influxdb .InfluxDBException .DatabaseNotFoundException ;
4
+ import org .junit .jupiter .api .Assertions ;
5
+ import org .junit .jupiter .api .Test ;
6
+ import org .junit .platform .runner .JUnitPlatform ;
7
+ import org .junit .runner .RunWith ;
8
+
9
+ /**
10
+ * Test cases for InfluxDBException
11
+ *
12
+ * @author hoan.le [at] bonitoo.io
13
+ *
14
+ */
15
+
16
+ @ RunWith (JUnitPlatform .class )
17
+ public class InfluxDBExceptionTest {
18
+
19
+ @ Test
20
+ public void testBuildExceptionForMessagePackErrorState () {
21
+ DatabaseNotFoundException dbex = (DatabaseNotFoundException ) InfluxDBException
22
+ .buildExceptionForErrorState (InfluxDBExceptionTest .class .getResourceAsStream ("msgpack_errorBody.bin" ));
23
+
24
+ Assertions .assertEquals ("database not found: \" abc\" " , dbex .getMessage ());
25
+
26
+ InfluxDBException ex = InfluxDBException .buildExceptionForErrorState (InfluxDBExceptionTest .class .getResourceAsStream ("invalid_msgpack_errorBody.bin" ));
27
+ Assertions .assertTrue (ex .getCause () instanceof ClassCastException );
28
+
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ e not found: "abc"
Original file line number Diff line number Diff line change
1
+ ��error�database not found: "abc"
You can’t perform that action at this time.
0 commit comments