Skip to content

Commit ce32b11

Browse files
committed
Merge pull request apache#555 from datastax/java833
JAVA-833: Improve message when a nested type can't be serialized.
2 parents 12d0bfb + ad4c22a commit ce32b11

4 files changed

Lines changed: 13 additions & 15 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

driver-core/src/main/java/com/datastax/driver/core/DataType.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,11 @@ public static ByteBuffer serializeValue(Object value, ProtocolVersion protocolVe
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

driver-core/src/main/java/com/datastax/driver/core/SimpleStatement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static ByteBuffer[] convert(Object[] values, ProtocolVersion protocolVer
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;

driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static TupleCodec tupleOf(TupleType type) {
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 @@ static DataType getDataTypeFor(Object value) {
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,27 +210,25 @@ static DataType getDataTypeFor(Object value) {
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) {
217217
Set<?> s = (Set<?>) value;
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) {
225225
Map<?, ?> m = (Map<?, ?>) value;
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 @@ static DataType getDataTypeFor(Object value) {
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) {

0 commit comments

Comments
 (0)