@@ -72,6 +72,8 @@ class ServerDescription {
7272 private final int minWireVersion ;
7373 private final int maxWireVersion ;
7474
75+ private Throwable exception ;
76+
7577 static class Builder {
7678 private ServerAddress address ;
7779 private ServerType type = Unknown ;
@@ -90,6 +92,7 @@ static class Builder {
9092 private ServerVersion version = new ServerVersion ();
9193 private int minWireVersion = 0 ;
9294 private int maxWireVersion = 0 ;
95+ private Throwable exception ;
9396
9497 // CHECKSTYLE:OFF
9598 public Builder address (final ServerAddress address ) {
@@ -178,6 +181,11 @@ public Builder maxWireVersion(final int maxWireVersion) {
178181 return this ;
179182 }
180183
184+ public Builder exception (final Throwable exception ) {
185+ this .exception = exception ;
186+ return this ;
187+ }
188+
181189 public ServerDescription build () {
182190 return new ServerDescription (this );
183191 }
@@ -349,6 +357,10 @@ public long getAverageLatencyNanos() {
349357 return averageLatencyNanos ;
350358 }
351359
360+ public Throwable getException () {
361+ return exception ;
362+ }
363+
352364 /**
353365 * Returns true if this instance is equals to @code{o}. Note that equality is defined to NOT include the average ping time.
354366 *
@@ -415,6 +427,19 @@ public boolean equals(final Object o) {
415427 return false ;
416428 }
417429
430+ // Compare class equality and message as exceptions rarely override equals
431+ Class thisExceptionClass = exception != null ? exception .getClass () : null ;
432+ Class thatExceptionClass = that .exception != null ? that .exception .getClass () : null ;
433+ if (thisExceptionClass != null ? !thisExceptionClass .equals (thatExceptionClass ) : thatExceptionClass != null ) {
434+ return false ;
435+ }
436+
437+ String thisExceptionMessage = exception != null ? exception .getMessage () : null ;
438+ String thatExceptionMessage = that .exception != null ? that .exception .getMessage () : null ;
439+ if (thisExceptionMessage != null ? !thisExceptionMessage .equals (thatExceptionMessage ) : thatExceptionMessage != null ) {
440+ return false ;
441+ }
442+
418443 return true ;
419444 }
420445
@@ -437,6 +462,8 @@ public int hashCode() {
437462 result = 31 * result + version .hashCode ();
438463 result = 31 * result + minWireVersion ;
439464 result = 31 * result + maxWireVersion ;
465+ result = 31 * result + (exception == null ? 0 : exception .getClass ().hashCode ());
466+ result = 31 * result + (exception == null ? 0 : exception .getMessage ().hashCode ());
440467 return result ;
441468 }
442469
@@ -467,12 +494,30 @@ public String getShortDescription() {
467494 return "{"
468495 + "address=" + address
469496 + ", type=" + type
470- + (tags .isEmpty () ? "" : tags )
497+ + (tags .isEmpty () ? "" : ", " + tags )
471498 + (state == Connected ? (", averageLatency=" + getAverageLatencyFormattedInMilliseconds () + " ms" ) : "" )
472499 + ", state=" + state
500+ + (exception == null ? "" : ", exception=" + translateExceptionToString ())
473501 + '}' ;
474502 }
475503
504+ private String translateExceptionToString () {
505+ StringBuilder builder = new StringBuilder ();
506+ builder .append ("{" );
507+ builder .append (exception );
508+ builder .append ("}" );
509+ Throwable cur = exception .getCause ();
510+ while (cur != null ) {
511+ builder .append (", caused by " );
512+ builder .append ("{" );
513+ builder .append (cur );
514+ builder .append ("}" );
515+ cur = cur .getCause ();
516+ }
517+
518+ return builder .toString ();
519+ }
520+
476521 private String getAverageLatencyFormattedInMilliseconds () {
477522 return new DecimalFormat ("#0.0" ).format (averageLatencyNanos / 1000.0 / 1000.0 );
478523 }
@@ -495,5 +540,6 @@ private String getAverageLatencyFormattedInMilliseconds() {
495540 ok = builder .ok ;
496541 minWireVersion = builder .minWireVersion ;
497542 maxWireVersion = builder .maxWireVersion ;
543+ exception = builder .exception ;
498544 }
499545}
0 commit comments