Skip to content

Commit 61fab0a

Browse files
committed
fix issue #505 : Error messages from server not parsed correctly when using msgpack
1 parent 3d387c0 commit 61fab0a

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

src/main/java/org/influxdb/InfluxDBException.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package org.influxdb;
22

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+
310
import com.squareup.moshi.JsonAdapter;
411
import com.squareup.moshi.Moshi;
512

@@ -168,4 +175,20 @@ public static InfluxDBException buildExceptionForErrorState(final String errorBo
168175
return new InfluxDBException(errorBody);
169176
}
170177
}
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+
}
171194
}

src/main/java/org/influxdb/impl/InfluxDBImpl.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,11 @@ private <T> T execute(final Call<T> call) {
696696
return response.body();
697697
}
698698
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+
}
700704
}
701705
} catch (IOException e) {
702706
throw new InfluxDBIOException(e);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
e not found: "abc"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
��error�database not found: "abc"

0 commit comments

Comments
 (0)