@@ -72,6 +72,8 @@ class ServerDescription {
72
72
private final int minWireVersion ;
73
73
private final int maxWireVersion ;
74
74
75
+ private Throwable exception ;
76
+
75
77
static class Builder {
76
78
private ServerAddress address ;
77
79
private ServerType type = Unknown ;
@@ -90,6 +92,7 @@ static class Builder {
90
92
private ServerVersion version = new ServerVersion ();
91
93
private int minWireVersion = 0 ;
92
94
private int maxWireVersion = 0 ;
95
+ private Throwable exception ;
93
96
94
97
// CHECKSTYLE:OFF
95
98
public Builder address (final ServerAddress address ) {
@@ -178,6 +181,11 @@ public Builder maxWireVersion(final int maxWireVersion) {
178
181
return this ;
179
182
}
180
183
184
+ public Builder exception (final Throwable exception ) {
185
+ this .exception = exception ;
186
+ return this ;
187
+ }
188
+
181
189
public ServerDescription build () {
182
190
return new ServerDescription (this );
183
191
}
@@ -349,6 +357,10 @@ public long getAverageLatencyNanos() {
349
357
return averageLatencyNanos ;
350
358
}
351
359
360
+ public Throwable getException () {
361
+ return exception ;
362
+ }
363
+
352
364
/**
353
365
* Returns true if this instance is equals to @code{o}. Note that equality is defined to NOT include the average ping time.
354
366
*
@@ -415,6 +427,19 @@ public boolean equals(final Object o) {
415
427
return false ;
416
428
}
417
429
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
+
418
443
return true ;
419
444
}
420
445
@@ -437,6 +462,8 @@ public int hashCode() {
437
462
result = 31 * result + version .hashCode ();
438
463
result = 31 * result + minWireVersion ;
439
464
result = 31 * result + maxWireVersion ;
465
+ result = 31 * result + (exception == null ? 0 : exception .getClass ().hashCode ());
466
+ result = 31 * result + (exception == null ? 0 : exception .getMessage ().hashCode ());
440
467
return result ;
441
468
}
442
469
@@ -467,12 +494,30 @@ public String getShortDescription() {
467
494
return "{"
468
495
+ "address=" + address
469
496
+ ", type=" + type
470
- + (tags .isEmpty () ? "" : tags )
497
+ + (tags .isEmpty () ? "" : ", " + tags )
471
498
+ (state == Connected ? (", averageLatency=" + getAverageLatencyFormattedInMilliseconds () + " ms" ) : "" )
472
499
+ ", state=" + state
500
+ + (exception == null ? "" : ", exception=" + translateExceptionToString ())
473
501
+ '}' ;
474
502
}
475
503
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
+
476
521
private String getAverageLatencyFormattedInMilliseconds () {
477
522
return new DecimalFormat ("#0.0" ).format (averageLatencyNanos / 1000.0 / 1000.0 );
478
523
}
@@ -495,5 +540,6 @@ private String getAverageLatencyFormattedInMilliseconds() {
495
540
ok = builder .ok ;
496
541
minWireVersion = builder .minWireVersion ;
497
542
maxWireVersion = builder .maxWireVersion ;
543
+ exception = builder .exception ;
498
544
}
499
545
}
0 commit comments