Skip to content

Commit 39e301d

Browse files
boly38mp911de
authored andcommitted
Translate MongoSocketException subclasses to DataAccessResourceFailureException.
Closes #3568 Original pull request: #3569.
1 parent a7d865e commit 39e301d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoExceptionTranslator.java

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18+
import com.mongodb.MongoSocketException;
1819
import java.util.Arrays;
1920
import java.util.Collections;
2021
import java.util.HashSet;
@@ -49,6 +50,7 @@
4950
* @author Oliver Gierke
5051
* @author Michal Vich
5152
* @author Christoph Strobl
53+
* @author Brice Vandeputte
5254
*/
5355
public class MongoExceptionTranslator implements PersistenceExceptionTranslator {
5456

@@ -78,6 +80,10 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
7880
throw new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
7981
}
8082

83+
if (ex instanceof MongoSocketException) {
84+
return new DataAccessResourceFailureException(ex.getMessage(), ex);
85+
}
86+
8187
String exception = ClassUtils.getShortName(ClassUtils.getUserClass(ex.getClass()));
8288

8389
if (DUPLICATE_KEY_EXCEPTIONS.contains(exception)) {
@@ -88,6 +94,7 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
8894
return new DataAccessResourceFailureException(ex.getMessage(), ex);
8995
}
9096

97+
9198
if (RESOURCE_USAGE_EXCEPTIONS.contains(exception)) {
9299
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
93100
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoExceptionTranslatorUnitTests.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import java.net.UnknownHostException;
20+
import com.mongodb.MongoSocketReadTimeoutException;
21+
import com.mongodb.MongoSocketWriteException;
2122

2223
import org.bson.BsonDocument;
2324
import org.junit.jupiter.api.BeforeEach;
@@ -45,9 +46,11 @@
4546
* @author Michal Vich
4647
* @author Oliver Gierke
4748
* @author Christoph Strobl
49+
* @author Brice Vandeputte
4850
*/
4951
public class MongoExceptionTranslatorUnitTests {
5052

53+
public static final String EXCEPTION_MESSAGE = "IOException";
5154
MongoExceptionTranslator translator;
5255

5356
@BeforeEach
@@ -68,13 +71,30 @@ public void translateDuplicateKey() {
6871
public void translateSocketException() {
6972

7073
expectExceptionWithCauseMessage(
71-
translator.translateExceptionIfPossible(new MongoSocketException("IOException", new ServerAddress())),
72-
DataAccessResourceFailureException.class, "IOException");
74+
translator.translateExceptionIfPossible(new MongoSocketException(EXCEPTION_MESSAGE, new ServerAddress())),
75+
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);
76+
77+
}
78+
79+
@Test // GH-3568
80+
public void translateSocketChildrenExceptions() {
81+
82+
expectExceptionWithCauseMessage(
83+
translator.translateExceptionIfPossible(
84+
new MongoSocketWriteException("intermediate message", new ServerAddress(), new Exception(EXCEPTION_MESSAGE))
85+
),
86+
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);
87+
88+
expectExceptionWithCauseMessage(
89+
translator.translateExceptionIfPossible(
90+
new MongoSocketReadTimeoutException("intermediate message", new ServerAddress(), new Exception(EXCEPTION_MESSAGE))
91+
),
92+
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);
7393

7494
}
7595

7696
@Test
77-
public void translateCursorNotFound() throws UnknownHostException {
97+
public void translateCursorNotFound() {
7898

7999
expectExceptionWithCauseMessage(
80100
translator.translateExceptionIfPossible(new MongoCursorNotFoundException(1L, new ServerAddress())),

0 commit comments

Comments
 (0)