25
25
26
26
import com .arangodb .ArangoDBException ;
27
27
import com .arangodb .springframework .ArangoUncategorizedException ;
28
+ import org .springframework .lang .Nullable ;
28
29
29
30
import java .util .Map ;
30
31
import java .util .function .BiFunction ;
31
32
import java .util .function .Predicate ;
32
33
34
+ import static com .arangodb .springframework .core .util .ArangoErrors .*;
33
35
import static java .util .Map .entry ;
34
36
35
37
/**
36
38
* Translate any {@link ArangoDBException} to the appropriate {@link DataAccessException} using response code and error number.
37
39
*
38
- * @see <a href="https://github.com/arangodb/arangodb/blob/devel/lib/Basics/errors.dat">General ArangoDB storage errors</a>
39
40
* @author Mark Vollmary
40
41
* @author Christian Lechner
41
42
* @author Arne Burmeister
42
43
*/
43
44
public class ArangoExceptionTranslator implements PersistenceExceptionTranslator {
44
45
45
46
@ Override
47
+ @ Nullable
46
48
public DataAccessException translateExceptionIfPossible (final RuntimeException ex ) {
47
- DataAccessException dae = null ;
48
49
if (ex instanceof DataAccessException exception ) {
49
- dae = exception ;
50
- } else if (ex instanceof ArangoDBException exception ) {
50
+ return exception ;
51
+ }
52
+ if (ex instanceof ArangoDBException exception ) {
51
53
final Integer responseCode = exception .getResponseCode ();
52
- if (responseCode != null ) {
53
- BiFunction <String , ArangoDBException , DataAccessException > constructor = switch (responseCode ) {
54
- case ArangoErrors .ERROR_HTTP_UNAUTHORIZED , ArangoErrors .ERROR_HTTP_FORBIDDEN -> PermissionDeniedDataAccessException ::new ;
55
- case ArangoErrors .ERROR_HTTP_BAD_PARAMETER , ArangoErrors .ERROR_HTTP_METHOD_NOT_ALLOWED -> InvalidDataAccessApiUsageException ::new ;
56
- case ArangoErrors .ERROR_HTTP_NOT_FOUND -> mostSpecific (exception , Map .ofEntries (
57
- entry (hasErrorNumber (1202 ), DataRetrievalFailureException ::new )
58
- ), InvalidDataAccessResourceUsageException ::new );
59
- case ArangoErrors .ERROR_HTTP_CONFLICT -> mostSpecific (exception , Map .ofEntries (
60
- entry (hasErrorNumber (1200 ).and (errorMessageContains ("write-write conflict" )), TransientDataAccessResourceException ::new ),
61
- entry (hasErrorNumber (1200 ).and (errorMessageContains ("_rev" )), OptimisticLockingFailureException ::new ),
62
- entry (hasErrorNumber (1210 ).and (errorMessageContains ("_key" )), DuplicateKeyException ::new )
63
- ),
64
- DataIntegrityViolationException ::new );
65
- case ArangoErrors .ERROR_HTTP_PRECONDITION_FAILED -> OptimisticLockingFailureException ::new ;
66
- case ArangoErrors .ERROR_HTTP_SERVICE_UNAVAILABLE -> DataAccessResourceFailureException ::new ;
67
- default -> ArangoUncategorizedException ::new ;
68
- };
69
- return constructor .apply (exception .getMessage (), exception );
54
+ if (responseCode == null ) {
55
+ return new ArangoUncategorizedException (exception .getMessage (), exception );
70
56
}
57
+ BiFunction <String , ArangoDBException , DataAccessException > constructor = switch (responseCode ) {
58
+ case ERROR_HTTP_UNAUTHORIZED , ERROR_HTTP_FORBIDDEN -> PermissionDeniedDataAccessException ::new ;
59
+ case ERROR_HTTP_BAD_PARAMETER , ERROR_HTTP_METHOD_NOT_ALLOWED -> InvalidDataAccessApiUsageException ::new ;
60
+ case ERROR_HTTP_NOT_FOUND -> mostSpecific (exception , Map .ofEntries (
61
+ entry (hasErrorNumber (ERROR_ARANGO_DOCUMENT_NOT_FOUND ), DataRetrievalFailureException ::new )
62
+ ), InvalidDataAccessResourceUsageException ::new );
63
+ case ERROR_HTTP_CONFLICT -> mostSpecific (exception , Map .ofEntries (
64
+ entry (hasErrorNumber (ERROR_ARANGO_CONFLICT ).and (errorMessageContains ("write-write" )), TransientDataAccessResourceException ::new ),
65
+ entry (hasErrorNumber (ERROR_ARANGO_CONFLICT ).and (errorMessageContains ("_rev" )), OptimisticLockingFailureException ::new ),
66
+ entry (hasErrorNumber (ERROR_ARANGO_UNIQUE_CONSTRAINT_VIOLATED ).and (errorMessageContains ("_key" )), DuplicateKeyException ::new )
67
+ ),
68
+ DataIntegrityViolationException ::new );
69
+ case ERROR_HTTP_PRECONDITION_FAILED -> OptimisticLockingFailureException ::new ;
70
+ case ERROR_HTTP_SERVICE_UNAVAILABLE -> DataAccessResourceFailureException ::new ;
71
+ default -> ArangoUncategorizedException ::new ;
72
+ };
73
+ return constructor .apply (exception .getMessage (), exception );
71
74
}
72
- if (dae == null ) {
73
- throw ex ;
74
- }
75
- return dae ;
75
+ return null ;
76
76
}
77
77
78
78
private static BiFunction <String , ArangoDBException , DataAccessException > mostSpecific (ArangoDBException exception ,
@@ -81,7 +81,7 @@ private static BiFunction<String, ArangoDBException, DataAccessException> mostSp
81
81
return specific .entrySet ().stream ()
82
82
.filter (entry -> entry .getKey ().test (exception ))
83
83
.map (Map .Entry ::getValue )
84
- .findAny ()
84
+ .findFirst ()
85
85
.orElse (fallback );
86
86
}
87
87
0 commit comments