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
/**
43
45
public class ArangoExceptionTranslator implements PersistenceExceptionTranslator {
44
46
45
47
@ Override
48
+ @ Nullable
46
49
public DataAccessException translateExceptionIfPossible (final RuntimeException ex ) {
47
- DataAccessException dae = null ;
48
50
if (ex instanceof DataAccessException exception ) {
49
- dae = exception ;
51
+ return exception ;
50
52
} else if (ex instanceof ArangoDBException exception ) {
51
53
final Integer responseCode = exception .getResponseCode ();
52
- if (responseCode != null ) {
54
+ if (responseCode == null ) {
55
+ return new ArangoUncategorizedException (exception .getMessage (), exception );
56
+ } else {
53
57
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
+ 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 )
58
62
), 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
+ 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 )
63
67
),
64
68
DataIntegrityViolationException ::new );
65
- case ArangoErrors . ERROR_HTTP_PRECONDITION_FAILED -> OptimisticLockingFailureException ::new ;
66
- case ArangoErrors . ERROR_HTTP_SERVICE_UNAVAILABLE -> DataAccessResourceFailureException ::new ;
69
+ case ERROR_HTTP_PRECONDITION_FAILED -> OptimisticLockingFailureException ::new ;
70
+ case ERROR_HTTP_SERVICE_UNAVAILABLE -> DataAccessResourceFailureException ::new ;
67
71
default -> ArangoUncategorizedException ::new ;
68
72
};
69
73
return constructor .apply (exception .getMessage (), exception );
70
74
}
71
75
}
72
- if (dae == null ) {
73
- throw ex ;
74
- }
75
- return dae ;
76
+ return null ;
76
77
}
77
78
78
79
private static BiFunction <String , ArangoDBException , DataAccessException > mostSpecific (ArangoDBException exception ,
@@ -81,7 +82,7 @@ private static BiFunction<String, ArangoDBException, DataAccessException> mostSp
81
82
return specific .entrySet ().stream ()
82
83
.filter (entry -> entry .getKey ().test (exception ))
83
84
.map (Map .Entry ::getValue )
84
- .findAny ()
85
+ .findFirst ()
85
86
.orElse (fallback );
86
87
}
87
88
0 commit comments