Elffuss BraidHub

kikocis/java-driver public

JAVA-833: Improve message when a nested type can't be serialized.
Changeset0746b46f6487a81dc83c607e67dc31281780e78d6e7e8895eb1b39d0a1e25fa4
Authorolim7t <omichallat+github@gmail.com> · 2015-12-10 12:12 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-02-23 17:41 +01:00
Parents4bb946c55b59

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -11,6 +11,7 @@
1111 - [improvement] JAVA-1030: Log token to replica map computation times.
1212 - [bug] JAVA-1039: Minor bugs in Event Debouncer.
1313 - [improvement] JAVA-843: Disable frozen checks in mapper.
14+- [improvement] JAVA-833: Improve message when a nested type can't be serialized.
1415
1516 Merged from 2.0 branch:
1617
modified driver-core/src/main/java/com/datastax/driver/core/DataType.java +3 −4
@@ -713,12 +713,11 @@
713713 if (value == null)
714714 return null;
715715
716- DataType dt = TypeCodec.getDataTypeFor(value);
717- if (dt == null)
718- throw new IllegalArgumentException(String.format("Value of type %s does not correspond to any CQL3 type", value.getClass()));
719-
720716 try {
717+ DataType dt = TypeCodec.getDataTypeFor(value);
721718 return dt.serialize(value, protocolVersion);
719+ } catch (IllegalArgumentException e) {
720+ throw new IllegalArgumentException(String.format("Could not serialize value of type %s", value.getClass()), e);
722721 } catch (InvalidTypeException e) {
723722 // In theory we couldn't get that if getDataTypeFor does his job correctly,
724723 // but there is no point in sending an exception that the user won't expect if we're
modified driver-core/src/main/java/com/datastax/driver/core/SimpleStatement.java +1 −1
@@ -96,7 +96,7 @@
9696 serializedValues[i] = DataType.serializeValue(value, protocolVersion);
9797 } catch (IllegalArgumentException e) {
9898 // Catch and rethrow to provide a more helpful error message (one that include which value is bad)
99- throw new IllegalArgumentException(String.format("Value %d of type %s does not correspond to any CQL3 type", i, value.getClass()));
99+ throw new IllegalArgumentException(String.format("Could not serialize value %d of type %s", i, value.getClass()), e.getCause());
100100 }
101101 }
102102 return serializedValues;
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +8 −10
@@ -159,7 +159,7 @@
159159 }
160160
161161 /* This is ugly, but not sure how we can do much better/faster
162- * Returns null if it's doesn't correspond to a known type.
162+ * Throws IllegalArgumentException if it's doesn't correspond to a known type.
163163 *
164164 * Also, note that this only a dataType that is fit for the value,
165165 * but for instance, for a UUID, this will return DataType.uuid() but
@@ -187,7 +187,7 @@
187187 return DataType.decimal();
188188 if (value instanceof BigInteger)
189189 return DataType.varint();
190- return null;
190+ throw new IllegalArgumentException("Type " + value.getClass().getName() + " does not correspond to any CQL type");
191191 }
192192
193193 if (value instanceof String)
@@ -210,7 +210,7 @@
210210 if (l.isEmpty())
211211 return DataType.list(DataType.blob());
212212 DataType eltType = getDataTypeFor(l.get(0));
213- return eltType == null ? null : DataType.list(eltType);
213+ return DataType.list(eltType);
214214 }
215215
216216 if (value instanceof Set) {
@@ -218,7 +218,7 @@
218218 if (s.isEmpty())
219219 return DataType.set(DataType.blob());
220220 DataType eltType = getDataTypeFor(s.iterator().next());
221- return eltType == null ? null : DataType.set(eltType);
221+ return DataType.set(eltType);
222222 }
223223
224224 if (value instanceof Map) {
@@ -226,11 +226,9 @@
226226 if (m.isEmpty())
227227 return DataType.map(DataType.blob(), DataType.blob());
228228 Map.Entry<?, ?> e = m.entrySet().iterator().next();
229- DataType keyType = getDataTypeFor(e.getKey());
230- DataType valueType = getDataTypeFor(e.getValue());
231- return keyType == null || valueType == null
232- ? null
233- : DataType.map(keyType, valueType);
229+ return DataType.map(
230+ getDataTypeFor(e.getKey()),
231+ getDataTypeFor(e.getValue()));
234232 }
235233
236234 if (value instanceof UDTValue) {
@@ -241,7 +239,7 @@
241239 return ((TupleValue) value).getType();
242240 }
243241
244- return null;
242+ throw new IllegalArgumentException("Type " + value.getClass().getName() + " does not correspond to any CQL type");
245243 }
246244
247245 private static ByteBuffer pack(List<ByteBuffer> buffers, int elements, ProtocolVersion version) {