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

Skip to content

Commit 20658a0

Browse files
committed
#281 declare error constants, translator returns null for unsupported exceptions
1 parent c7abc82 commit 20658a0

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,63 @@
2828
public class ArangoErrors {
2929

3030
/**
31-
* bad parameter. Will be raised when the HTTP request does not fulfill the requirements.
31+
* Bad parameter, will be raised when the HTTP request does not fulfill the requirements.
3232
*/
3333
public static final int ERROR_HTTP_BAD_PARAMETER = 400;
3434

3535
/**
36-
* unauthorized. Will be raised when authorization is required but the user is not authorized.
36+
* Unauthorized, will be raised when authorization is required but the user is not authorized.
3737
*/
3838
public static final int ERROR_HTTP_UNAUTHORIZED = 401;
3939

4040
/**
41-
* forbidden. Will be raised when the operation is forbidden.
41+
* Forbidden, will be raised when the operation is forbidden.
4242
*/
4343
public static final int ERROR_HTTP_FORBIDDEN = 403;
4444

4545
/**
46-
* not found. Will be raised when an URI is unknown.
46+
* Not found, will be raised when an URI is unknown.
4747
*/
4848
public static final int ERROR_HTTP_NOT_FOUND = 404;
4949

5050
/**
51-
* method not supported. Will be raised when an unsupported HTTP method is used for an operation.
51+
* Method not supported, will be raised when an unsupported HTTP method is used for an operation.
5252
*/
5353
public static final int ERROR_HTTP_METHOD_NOT_ALLOWED = 405;
5454

5555
/**
56-
* conflict. Will be raised when a conflict is encountered.
56+
* Conflict, will be raised when a conflict is encountered.
5757
*/
5858
public static final int ERROR_HTTP_CONFLICT = 409;
5959

6060
/**
61-
* precondition failed. Will be raised when a precondition for an HTTP request is not met.
61+
* Precondition failed, will be raised when a precondition for an HTTP request is not met.
6262
*/
6363
public static final int ERROR_HTTP_PRECONDITION_FAILED = 412;
6464

6565
/**
66-
* internal server error. Will be raised when an internal server is encountered.
66+
* Internal server error, will be raised when an internal server is encountered.
6767
*/
6868
public static final int ERROR_HTTP_SERVER_ERROR = 500;
6969

7070
/**
71-
* service unavailable. Will be raised when a service is temporarily unavailable.
71+
* Service unavailable, will be raised when a service is temporarily unavailable.
7272
*/
7373
public static final int ERROR_HTTP_SERVICE_UNAVAILABLE = 503;
7474

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

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
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
/**
@@ -43,36 +45,35 @@
4345
public class ArangoExceptionTranslator implements PersistenceExceptionTranslator {
4446

4547
@Override
48+
@Nullable
4649
public DataAccessException translateExceptionIfPossible(final RuntimeException ex) {
47-
DataAccessException dae = null;
4850
if (ex instanceof DataAccessException exception) {
49-
dae = exception;
51+
return exception;
5052
} else if (ex instanceof ArangoDBException exception) {
5153
final Integer responseCode = exception.getResponseCode();
52-
if (responseCode != null) {
54+
if (responseCode == null) {
55+
return new ArangoUncategorizedException(exception.getMessage(), exception);
56+
} else {
5357
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)
5862
), 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)
6367
),
6468
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;
6771
default -> ArangoUncategorizedException::new;
6872
};
6973
return constructor.apply(exception.getMessage(), exception);
7074
}
7175
}
72-
if (dae == null) {
73-
throw ex;
74-
}
75-
return dae;
76+
return null;
7677
}
7778

7879
private static BiFunction<String, ArangoDBException, DataAccessException> mostSpecific(ArangoDBException exception,
@@ -81,7 +82,7 @@ private static BiFunction<String, ArangoDBException, DataAccessException> mostSp
8182
return specific.entrySet().stream()
8283
.filter(entry -> entry.getKey().test(exception))
8384
.map(Map.Entry::getValue)
84-
.findAny()
85+
.findFirst()
8586
.orElse(fallback);
8687
}
8788

0 commit comments

Comments
 (0)
0