Skip to content

Commit

Permalink
Improve MongoExceptionTranslator to accept MongoSocketException child…
Browse files Browse the repository at this point in the history
…ren.

Closes spring-projects#3568
  • Loading branch information
boly38 committed Mar 1, 2021
1 parent ffaa7ca commit df3f71d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.core;

import com.mongodb.MongoSocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -49,6 +50,7 @@
* @author Oliver Gierke
* @author Michal Vich
* @author Christoph Strobl
* @author Brice Vandeputte
*/
public class MongoExceptionTranslator implements PersistenceExceptionTranslator {

Expand Down Expand Up @@ -78,6 +80,10 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
throw new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}

if (ex instanceof MongoSocketException) {
return new DataAccessResourceFailureException(ex.getMessage(), ex);
}

String exception = ClassUtils.getShortName(ClassUtils.getUserClass(ex.getClass()));

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


if (RESOURCE_USAGE_EXCEPTIONS.contains(exception)) {
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

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

import java.net.UnknownHostException;
import com.mongodb.MongoSocketReadTimeoutException;
import com.mongodb.MongoSocketWriteException;

import org.bson.BsonDocument;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -45,9 +46,11 @@
* @author Michal Vich
* @author Oliver Gierke
* @author Christoph Strobl
* @author Brice Vandeputte
*/
public class MongoExceptionTranslatorUnitTests {

public static final String EXCEPTION_MESSAGE = "IOException";
MongoExceptionTranslator translator;

@BeforeEach
Expand All @@ -68,13 +71,30 @@ public void translateDuplicateKey() {
public void translateSocketException() {

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(new MongoSocketException("IOException", new ServerAddress())),
DataAccessResourceFailureException.class, "IOException");
translator.translateExceptionIfPossible(new MongoSocketException(EXCEPTION_MESSAGE, new ServerAddress())),
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);

}

@Test // GH-3568
public void translateSocketChildrenExceptions() {

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(
new MongoSocketWriteException("intermediate message", new ServerAddress(), new Exception(EXCEPTION_MESSAGE))
),
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(
new MongoSocketReadTimeoutException("intermediate message", new ServerAddress(), new Exception(EXCEPTION_MESSAGE))
),
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);

}

@Test
public void translateCursorNotFound() throws UnknownHostException {
public void translateCursorNotFound() {

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(new MongoCursorNotFoundException(1L, new ServerAddress())),
Expand Down

0 comments on commit df3f71d

Please sign in to comment.