3232import org .bson .BsonDocument ;
3333import org .bson .BsonValue ;
3434
35+ import com .mongodb .lang .Nullable ;
36+
3537public 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