8000 #281 declare error constants, translator returns null for unsupported… · arangodb/spring-data@9853b2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9853b2c

Browse files
committed
#281 declare error constants, translator returns null for unsupported exceptions
1 parent 6bc55da commit 9853b2c

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

src/main/java/com/arangodb/springframework/core/util/ArangoErrors.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,71 @@
2121
package com.arangodb.springframework.core.util;
2222

2323
/**
24+
* @see <a href="https://github.com/arangodb/arangodb/blob/devel/lib/Basics/errors.dat">General ArangoDB storage errors</a>
2425
* @author Mark Vollmary
2526
* @author Christian Lechner
26-
*
27+
* @author Arne Burmeister
2728
*/
2829
public class ArangoErrors {
2930

3031
/**
31-
* bad parameter. Will be raised when the HTTP request does not fulfill the requirements.
32+
* Bad parameter, will be raised when the HTTP request does not fulfill the requirements.
3233
*/
3334
public static final int ERROR_HTTP_BAD_PARAMETER = 400;
3435

3536
/**
36-
* unauthorized. Will be raised when authorization is required but the user is not authorized.
37+
* Unauthorized, will be raised when authorization is required but the user is not authorized.
3738
*/
3839
public static final int ERROR_HTTP_UNAUTHORIZED = 401;
3940

4041
/**
41-
* forbidden. Will be raised when the operation is forbidden.
42+
* Forbidden, will be raised when the operation is forbidden.
4243
*/
4344
public static final int ERROR_HTTP_FORBIDDEN = 403;
4445

4546
/**
46-
* not found. Will be raised when an URI is unknown.
47+
* Not found, will be raised when an URI is unknown.
4748
*/
4849
public static final int ERROR_HTTP_NOT_FOUND = 404;
4950

5051
/**
51-
* method not supported. Will be raised when an unsupported HTTP method is used for an operation.
52+
* Method not supported, will be raised when an unsupported HTTP method is used for an operation.
5253
*/
5354
public static final int ERROR_HTTP_METHOD_NOT_ALLOWED = 405;
5455

5556
/**
56-
* conflict. Will be raised when a conflict is encountered.
57+
* Conflict, will be raised when a conflict is encountered.
5758
*/
5859
public static final int ERROR_HTTP_CONFLICT = 409;
5960

6061
/**
61-
* precondition failed. Will be raised when a precondition for an HTTP request is not met.
62+
* Precondition failed, will be raised when a precondition for an HTTP request is not met.
6263
*/
6364
public static final int ERROR_HTTP_PRECONDITION_FAILED = 412;
6465

6566
/**
66-
* internal server error. Will be raised when an internal server is encountered.
67+
* Internal server error, will be raised when an internal server is encountered.
6768
*/
6869
public static final int ERROR_HTTP_SERVER_ERROR = 500;
6970

7071
/**
71-
* service unavailable. Will be raised when a service is temporarily unavailable.
72+
* Service unavailable, will be raised when a service is temporarily unavailable.
7273
*/
7374
public static final int ERROR_HTTP_SERVICE_UNAVAILABLE = 503;
7475

76+
/**
77+
* Conflict, will be raised when updating or deleting a document and a conflict has been detected.
78+
*/
79+
public static final int ERROR_ARANGO_CONFLICT = 1200;
80+
81+
/**
82+
* Document not found, will be raised when a document with a given identifier is unknown.
83+
*/
84+
public static final int ERROR_ARANGO_DOCUMENT_NOT_FOUND = 1202;
85+
86+
/**
87+
* Unique constraint violated, will be raised when there is a unique constraint violation.
88+
*/
89+
public static final int ERROR_ARANGO_UNIQUE_CONSTRAINT_VIOLATED = 1210;
90+
7591
}

src/main/java/com/arangodb/springframework/core/util/ArangoExceptionTranslator.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,54 @@
2525

2626
import com.arangodb.ArangoDBException;
2727
import com.arangodb.springframework.ArangoUncategorizedException;
28+
import org.springframework.lang.Nullable;
2829

2930
import java.util.Map;
3031
import java.util.function.BiFunction;
3132
import java.util.function.Predicate;
3233

34+
import static com.arangodb.springframework.core.util.ArangoErrors.*;
3335
import static java.util.Map.entry;
3436

3537
/**
3638
* Translate any {@link ArangoDBException} to the appropriate {@link DataAccessException} using response code and error number.
3739
*
38-
* @see <a href="https://github.com/arangodb/arangodb/blob/devel/lib/Basics/errors.dat">General ArangoDB storage errors</a>
3940
* @author Mark Vollmary
4041
* @author Christian Lechner
4142
* @author Arne Burmeister
4243
*/
4344
public class ArangoExceptionTranslator implements PersistenceExceptionTranslator {
4445

4546
@Override
47+
@Nullable
4648
public DataAccessException translateExceptionIfPossible(final RuntimeException ex) {
47-
DataAccessException dae = null;
4849
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) {
5153
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);
7056
}
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);
7174
}
72-
if (dae == null) {
73-
throw ex;
74-
}
75-
return dae;
75+
return null;
7676
}
7777

7878
private static BiFunction<String, ArangoDBException, DataAccessException> mostSpecific(ArangoDBException exception,
@@ -81,7 +81,7 @@ private static BiFunction<String, ArangoDBException, DataAccessException> mostSp
8181
return specific.entrySet().stream()
8282
.filter(entry -> entry.getKey().test(exception))
8383
.map(Map.Entry::getValue)
84-
.findAny()
84+
.findFirst()
8585
.orElse(fallback);
8686
}
8787

0 commit comments

Comments
 (0)
0