Skip to content

Commit 523cc7a

Browse files
authored
Improve "Could not convert value ..." messages (#140)
A viewer should not be left guessing whether the value was a null reference, or a "null" `CharSequence`.
1 parent 0c9dbf6 commit 523cc7a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/integrationTest/java/com/mongodb/kafka/connect/sink/MongoSinkTaskIntegrationTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ void testSinkCanHandleInvalidDocumentWhenErrorToleranceIsAll() {
338338
task.start(cfg);
339339

340340
DataException e = assertThrows(DataException.class, () -> task.put(sinkRecords));
341-
assertTrue(e.getMessage().contains("Could not convert value `a` into a BsonDocument"));
341+
assertTrue(
342+
e.getMessage()
343+
.contains("Could not convert value 'a' (java.lang.String) into a BsonDocument"));
342344
}
343345
}
344346

src/main/java/com/mongodb/kafka/connect/sink/converter/LazyBsonDocument.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.bson.BsonDocument;
3333
import org.bson.BsonValue;
3434

35+
import com.mongodb.lang.Nullable;
36+
3537
public class LazyBsonDocument extends BsonDocument {
3638
private static final long serialVersionUID = 1L;
3739

@@ -158,15 +160,21 @@ private BsonDocument getUnwrapped() {
158160
unwrapped = converter.apply(record.keySchema(), record.key());
159161
} catch (Exception e) {
160162
throw new DataException(
161-
format("Could not convert key `%s` into a BsonDocument.", record.key()), e);
163+
format(
164+
"Could not convert key %s into a BsonDocument.",
165+
unambiguousToString(record.key())),
166+
e);
162167
}
163168
break;
164169
case VALUE:
165170
try {
166171
unwrapped = converter.apply(record.valueSchema(), record.value());
167172
} catch (Exception e) {
168173
throw new DataException(
169-
format("Could not convert value `%s` into a BsonDocument.", record.value()), e);
174+
format(
175+
"Could not convert value %s into a BsonDocument.",
176+
unambiguousToString(record.value())),
177+
e);
170178
}
171179
break;
172180
default:
@@ -185,4 +193,15 @@ private Object writeReplace() {
185193
private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
186194
throw new InvalidObjectException("Proxy required");
187195
}
196+
197+
private static String unambiguousToString(@Nullable final Object v) {
198+
String vToString = String.valueOf(v);
199+
if (v == null) {
200+
return format("'%s' (null reference)", vToString);
201+
} else if (vToString.equals(String.valueOf((Object) null))) {
202+
return format("'%s' (%s, not a null reference)", vToString, v.getClass().getName());
203+
} else {
204+
return format("'%s' (%s)", vToString, v.getClass().getName());
205+
}
206+
}
188207
}

0 commit comments

Comments
 (0)