15
15
*/
16
16
package org .springframework .data .mongodb .core ;
17
17
18
+ import static java .lang .String .format ;
19
+ import static java .util .Arrays .asList ;
20
+
18
21
import java .util .Arrays ;
19
22
import java .util .Collections ;
20
23
import java .util .HashSet ;
49
52
* @author Oliver Gierke
50
53
* @author Michal Vich
51
54
* @author Christoph Strobl
55
+ * @author Brice Vandeputte
52
56
*/
53
57
public class MongoExceptionTranslator implements PersistenceExceptionTranslator {
58
+ /**
59
+ * translation matrix definitions:
60
+ * - adding {@code ClassShortName} will associate a given exception class to a translated group
61
+ * - adding {@code ClassShortName$children} will associate a given exception direct children to a translated group
62
+ */
54
63
55
64
private static final Set <String > DUPLICATE_KEY_EXCEPTIONS = new HashSet <>(
56
- Arrays . asList ("MongoException.DuplicateKey" , "DuplicateKeyException" ));
65
+ asList ("MongoException.DuplicateKey" , "DuplicateKeyException" ));
57
66
58
67
private static final Set <String > RESOURCE_FAILURE_EXCEPTIONS = new HashSet <>(
59
- Arrays .asList ("MongoException.Network" , "MongoSocketException" , "MongoException.CursorNotFound" ,
60
- "MongoCursorNotFoundException" , "MongoServerSelectionException" , "MongoTimeoutException" ));
68
+ asList ("MongoException.Network" , "MongoSocketException" , "MongoSocketException$children" ,
69
+ "MongoException.CursorNotFound" , "MongoCursorNotFoundException" , "MongoServerSelectionException" ,
70
+ "MongoTimeoutException" ));
61
71
62
72
private static final Set <String > RESOURCE_USAGE_EXCEPTIONS = new HashSet <>(
63
73
Collections .singletonList ("MongoInternalException" ));
64
74
65
75
private static final Set <String > DATA_INTEGRITY_EXCEPTIONS = new HashSet <>(
66
- Arrays . asList ("WriteConcernException" , "MongoWriteException" , "MongoBulkWriteException" ));
76
+ asList ("WriteConcernException" , "MongoWriteException" , "MongoBulkWriteException" ));
67
77
68
78
/*
69
79
* (non-Javadoc)
@@ -78,21 +88,25 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
78
88
throw new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
79
89
}
80
90
81
- String exception = ClassUtils .getShortName (ClassUtils .getUserClass (ex .getClass ()));
91
+ Class <? extends RuntimeException > exceptionClass = ex .getClass ();
92
+ String exception = ClassUtils .getShortName (ClassUtils .getUserClass (exceptionClass ));
93
+ String exceptionParentMatcher = format ("%s$children" ,
94
+ ClassUtils .getShortName (ClassUtils .getUserClass (exceptionClass .getSuperclass ()))
95
+ );
82
96
83
- if (DUPLICATE_KEY_EXCEPTIONS . contains ( exception )) {
97
+ if (! Collections . disjoint ( DUPLICATE_KEY_EXCEPTIONS , asList ( exception , exceptionParentMatcher ) )) {
84
98
return new DuplicateKeyException (ex .getMessage (), ex );
85
99
}
86
100
87
- if (RESOURCE_FAILURE_EXCEPTIONS . contains ( exception )) {
101
+ if (! Collections . disjoint ( RESOURCE_FAILURE_EXCEPTIONS , asList ( exception , exceptionParentMatcher ) )) {
88
102
return new DataAccessResourceFailureException (ex .getMessage (), ex );
89
103
}
90
104
91
- if (RESOURCE_USAGE_EXCEPTIONS . contains ( exception )) {
105
+ if (! Collections . disjoint ( RESOURCE_USAGE_EXCEPTIONS , asList ( exception , exceptionParentMatcher ) )) {
92
106
return new InvalidDataAccessResourceUsageException (ex .getMessage (), ex );
93
107
}
94
108
95
- if (DATA_INTEGRITY_EXCEPTIONS . contains ( exception )) {
109
+ if (! Collections . disjoint ( DATA_INTEGRITY_EXCEPTIONS , asList ( exception , exceptionParentMatcher ) )) {
96
110
97
111
if (ex instanceof MongoServerException ) {
98
112
if (((MongoServerException ) ex ).getCode () == 11000 ) {
0 commit comments