Skip to content

Commit d4bc4c7

Browse files
committed
Copy error labels from cause to wrapping timeout exception per DRIVERS-3391 and fix timeout prose test assertions
1 parent 94a79db commit d4bc4c7

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

driver-sync/src/main/com/mongodb/client/internal/ClientSessionImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,13 @@ private static void backoff(final int transactionAttempt,
407407
}
408408

409409
private static MongoException wrapInMongoTimeoutException(final MongoException cause, final boolean timeoutMsConfigured) {
410-
return timeoutMsConfigured
410+
MongoException timeoutException = timeoutMsConfigured
411411
? createMongoTimeoutException(cause)
412412
: wrapInNonTimeoutMsMongoTimeoutException(cause);
413+
if (timeoutException != cause) {
414+
cause.getErrorLabels().forEach(timeoutException::addLabel);
415+
}
416+
return timeoutException;
413417
}
414418

415419
private static MongoTimeoutException wrapInNonTimeoutMsMongoTimeoutException(final MongoException cause) {

driver-sync/src/test/functional/com/mongodb/client/WithTransactionProseTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ public void testRetryTimeoutEnforcedTransientTransactionError() {
118118
}));
119119
fail("Test should have thrown an exception.");
120120
} catch (Exception e) {
121-
assertInstanceOf(MongoTimeoutException.class, e);
122-
MongoException cause = (MongoException) e.getCause();
121+
MongoTimeoutException exception = assertInstanceOf(MongoTimeoutException.class, e);
122+
assertTrue(exception.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL));
123+
MongoException cause = assertInstanceOf(MongoException.class, exception.getCause());
123124
assertEquals(errorMessage, cause.getMessage());
124-
assertTrue(cause.getErrorLabels().contains(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL));
125+
assertTrue(cause.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL));
125126
}
126127
}
127128

@@ -145,10 +146,11 @@ public void testRetryTimeoutEnforcedUnknownTransactionCommit() {
145146
}));
146147
fail("Test should have thrown an exception.");
147148
} catch (Exception e) {
148-
assertInstanceOf(MongoNodeIsRecoveringException.class, e.getCause());
149-
MongoNodeIsRecoveringException cause = (MongoNodeIsRecoveringException) e.getCause();
149+
MongoTimeoutException exception = assertInstanceOf(MongoTimeoutException.class, e);
150+
assertTrue(exception.hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL));
151+
MongoNodeIsRecoveringException cause = assertInstanceOf(MongoNodeIsRecoveringException.class, exception.getCause());
150152
assertEquals(91, cause.getCode());
151-
assertTrue(cause.getErrorLabels().contains(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL));
153+
assertTrue(cause.hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL));
152154
} finally {
153155
failPointAdminDb.runCommand(Document.parse("{'configureFailPoint': 'failCommand', 'mode': 'off'}"));
154156
}
@@ -176,11 +178,11 @@ public void testRetryTimeoutEnforcedTransientTransactionErrorOnCommit() {
176178
}));
177179
fail("Test should have thrown an exception.");
178180
} catch (Exception e) {
179-
assertEquals(-4, ((MongoException) e).getCode());
180-
assertInstanceOf(MongoCommandException.class, e.getCause());
181-
MongoCommandException cause = (MongoCommandException) e.getCause();
181+
MongoTimeoutException exception = assertInstanceOf(MongoTimeoutException.class, e);
182+
assertTrue(exception.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL));
183+
MongoCommandException cause = assertInstanceOf(MongoCommandException.class, exception.getCause());
182184
assertEquals(251, cause.getCode());
183-
assertTrue(cause.getErrorLabels().contains(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL));
185+
assertTrue(cause.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL));
184186
} finally {
185187
failPointAdminDb.runCommand(Document.parse("{'configureFailPoint': 'failCommand', 'mode': 'off'}"));
186188
}

0 commit comments

Comments
 (0)