8000 [DE-682] #281 better exception translation (#282) · arangodb/spring-data@6c27511 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c27511

Browse files
aburmeisrashtao
andauthored
[DE-682] #281 better exception translation (#282)
* #281 use data access utils for exception handling * #281 translate exceptions for query * #281 more specific exception translation * #281 test query exception translation * #281 declare error constants, translator returns null for unsupported exceptions * Update src/main/java/com/arangodb/springframework/core/util/ArangoExceptionTranslator.java --------- Co-authored-by: Michele Rastelli <rashtao@gmail.com>
1 parent ba4a37f commit 6c27511

File tree

7 files changed

+142
-105
lines changed

7 files changed

+142
-105
lines changed

src/main/java/com/arangodb/springframework/core/template/ArangoTemplate.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.springframework.context.expression.BeanFactoryAccessor;
6565
import org.springframework.context.expression.BeanFactoryResolver;
6666
import org.springframework.dao.DataAccessException;
67+
import org.springframework.dao.support.DataAccessUtils;
6768
import org.springframework.dao.support.PersistenceExceptionTranslator;
6869
import org.springframework.data.mapping.PersistentPropertyAccessor;
6970
import org.springframework.expression.Expression;
@@ -150,10 +151,6 @@ private ArangoDatabase db() {
150151
});
151152
}
152153

153-
private DataAccessException translateExceptionIfPossible(final RuntimeException exception) {
154-
return exceptionTranslator.translateExceptionIfPossible(exception);
155-
}
156-
157154
private ArangoCollection _collection(final String name) {
158155
return _collection(name, null, null);
159156
}
@@ -299,7 +296,7 @@ public ArangoDBVersion getVersion() throws DataAccessException {
299296
}
300297
return version;
301298
} catch (final ArangoDBException e) {
302-
throw translateExceptionIfPossible(e);
299+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
303300
}
304301
}
305302

@@ -327,7 +324,7 @@ public <T> ArangoCursor<T> query(final String query, final Map<String, Object> b
327324
ArangoCursor<JsonNode> cursor = db().query(query, JsonNode.class, bindVars == null ? null : prepareBindVars(bindVars), options);
328325
return new ArangoExtCursor<>(cursor, entityClass, converter, eventPublisher);
329326
} catch (final ArangoDBException e) {
330-
throw translateExceptionIfPossible(e);
327+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
331328
}
332329
}
333330

@@ -353,7 +350,7 @@ public MultiDocumentEntity<? extends DocumentEntity> delete(final Iterable<Objec
353350
try {
354351
result = _collection(entityClass).deleteDocuments(toJsonNodeCollection(values), options, entityClass);
355352
} catch (final ArangoDBException e) {
356-
throw translateExceptionIfPossible(e);
353+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
357354
}
358355

359356
potentiallyEmitAfterDeleteEvent(values, entityClass, result);
@@ -376,7 +373,7 @@ public DocumentEntity delete(final Object id, final Class<?> entityClass, final
376373
try {
377374
result = _collection(entityClass, id).deleteDocument(determineDocumentKeyFromId(id), options, entityClass);
378375
} catch (final ArangoDBException e) {
379-
throw translateExceptionIfPossible(e);
376+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
380377
}
381378

382379
potentiallyEmitEvent(new AfterDeleteEvent<>(id, entityClass));
@@ -398,7 +395,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> update(final Iterable<T
398395
try {
399396
result = _collection(entityClass).updateDocuments(toJsonNodeCollection(values), options);
400397
} catch (final ArangoDBException e) {
401-
throw translateExceptionIfPossible(e);
398+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
402399
}
403400

404401
updateDBFields(values, result);
@@ -423,7 +420,7 @@ public DocumentEntity update(final Object id, final Object value, final Document
423420
result = _collection(value.getClass(), id).updateDocument(determineDocumentKeyFromId(id), toJsonNode(value),
424421
options);
425422
} catch (final ArangoDBException e) {
426-
throw translateExceptionIfPossible(e);
423+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
427424
}
428425

429426
updateDBFields(value, result);
@@ -446,7 +443,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> replace(final Iterable<
446443
try {
447444
result = _collection(entityClass).replaceDocuments(toJsonNodeCollection(values), options);
448445
} catch (final ArangoDBException e) {
449-
throw translateExceptionIfPossible(e);
446+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
450447
}
451448

452449
updateDBFields(values, result);
@@ -470,7 +467,7 @@ public DocumentEntity replace(final Object id, final Object value, final Documen
470467
result = _collection(value.getClass(), id).replaceDocument(determineDocumentKeyFromId(id), toJsonNode(value),
471468
options);
472469
} catch (final ArangoDBException e) {
473-
throw translateExceptionIfPossible(e);
470+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
474471
}
475472

476473
updateDBFields(value, result);
@@ -491,7 +488,7 @@ public <T> Optional<T> find(final Object id, final Class<T> entityClass, final D
491488
JsonNode.class, options);
492489
return Optional.ofNullable(fromJsonNode(entityClass, doc));
493490
} catch (final ArangoDBException e) {
494-
throw translateExceptionIfPossible(e);
491+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
495492
}
496493
}
497494

@@ -516,7 +513,7 @@ public <T> Iterable<T> find(final Iterable<?> ids, final Class<T> entityClass)
516513
final MultiDocumentEntity<JsonNode> docs = _collection(entityClass).getDocuments(keys, JsonNode.class);
517514
return docs.getDocuments().stream().map(doc -> fromJsonNode(entityClass, doc)).collect(Collectors.toList());
518515
} catch (final ArangoDBException e) {
519-
throw translateExceptionIfPossible(e);
516+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
520517
}
521518
}
522519

@@ -530,7 +527,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> insert(final Iterable<T
530527
try {
531528
result = _collection(entityClass).insertDocuments(toJsonNodeCollection(values), options);
532529
} catch (final ArangoDBException e) {
533-
throw translateExceptionIfPossible(e);
530+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
534531
}
535532

536533
updateDBFields(values, result);
@@ -552,7 +549,7 @@ public DocumentEntity insert(final Object value, final DocumentCreateOptions opt
552549
try {
553550
result = _collection(value.getClass()).insertDocument(toJsonNode(value), options);
554551
} catch (final ArangoDBException e) {
555-
throw exceptionTranslator.translateExceptionIfPossible(e);
552+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
556553
}
557554

558555
updateDBFields(value, result);
@@ -574,7 +571,7 @@ public DocumentEntity insert(final String collectionName, final Object value, fi
574571
try {
575572
result = _collection(collectionName).insertDocument(toJsonNode(value), options);
576573
} catch (final ArangoDBException e) {
577-
throw exceptionTranslator.translateExceptionIfPossible(e);
574+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
578575
}
579576

580577
updateDBFields(value, result);
@@ -607,7 +604,7 @@ public <T> void repsert(final T value) throws DataAccessException {
607604
);
608605
result = it.hasNext() ? it.next() : null;
609606
} catch (final ArangoDBException e) {
610-
throw exceptionTranslator.translateExceptionIfPossible(e);
607+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
611608
}
612609

613610
updateDBFieldsFromObject(value, result);
@@ -635,7 +632,7 @@ public <T> void repsert(final Iterable<? extends T> values, final Class<T> entit
635632
entityClass
636633
).asListRemaining();
637634
} catch (final ArangoDBException e) {
638-
throw translateExceptionIfPossible(e);
635+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
639636
}
640637

641638
updateDBFieldsFromObjects(values, result);
@@ -715,7 +712,7 @@ public boolean exists(final Object id, final Class<?> entityClass) throws DataAc
715712
try {
716713
return _collection(entityClass).documentExists(determineDocumentKeyFromId(id));
717714
} catch (final ArangoDBException e) {
718-
throw translateExceptionIfPossible(e);
715+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
719716
}
720717
}
721718

@@ -725,7 +722,7 @@ public void dropDatabase() throws DataAccessException {
725722
try {
726723
db.drop();
727724
} catch (final ArangoDBException e) {
728-
throw translateExceptionIfPossible(e);
725+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
729726
}
730727
databaseCache.remove(db.name());
731728
collectionCache.keySet().stream().filter(key -> key.getDb().equals(db.name()))
@@ -762,7 +759,7 @@ public Iterable<UserEntity> getUsers() throws DataAccessException {
762759
try {
763760
return arango.getUsers();
764761
} catch (final ArangoDBException e) {
765-
throw translateExceptionIfPossible(e);
762+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
766763
}
767764
}
768765

src/main/java/com/arangodb/springframework/core/template/DefaultCollectionOperations.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Map;
2525

2626
import org.springframework.dao.DataAccessException;
27+
import org.springframework.dao.support.DataAccessUtils;
2728
import org.springframework.dao.support.PersistenceExceptionTranslator;
2829

2930
import com.arangodb.ArangoCollection;
@@ -55,10 +56,6 @@ protected DefaultCollectionOperations(final ArangoCollection collection,
5556
this.exceptionTranslator = exceptionTranslator;
5657
}
5758

58-
private DataAccessException translateExceptionIfPossible(final RuntimeException exception) {
59-
return exceptionTranslator.translateExceptionIfPossible(exception);
60-
}
61-
6259
@Override
6360
public String name() {
6461
return collection.name();
@@ -70,7 +67,7 @@ public void drop() throws DataAccessException {
7067
try {
7168
collection.drop();
7269
} catch (final ArangoDBException e) {
73-
throw translateExceptionIfPossible(e);
70+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
7471
}
7572
}
7673

@@ -79,7 +76,7 @@ public void truncate() throws DataAccessException {
7976
try {
8077
collection.truncate();
8178
} catch (final ArangoDBException e) {
82-
throw translateExceptionIfPossible(e);
79+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
8380
}
8481
}
8582

@@ -89,7 +86,7 @@ public long count() throws DataAccessException {
8986
final Long count = collection.count().getCount();
9087
return count != null ? count : -1L;
9188
} catch (final ArangoDBException e) {
92-
throw translateExceptionIfPossible(e);
89+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
9390
}
9491
}
9592

@@ -98,7 +95,7 @@ public CollectionPropertiesEntity getProperties() throws DataAccessException {
9895
try {
9996
return collection.getProperties();
10097
} catch (final ArangoDBException e) {
101-
throw translateExceptionIfPossible(e);
98+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
10299
}
103100
}
104101

@@ -107,7 +104,7 @@ public Collection<IndexEntity> getIndexes() throws DataAccessException {
107104
try {
108105
return collection.getIndexes();
109106
} catch (final ArangoDBException e) {
110-
throw translateExceptionIfPossible(e);
107+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
111108
}
112109
}
113110

@@ -117,7 +114,7 @@ public IndexEntity ensurePersistentIndex(final Iterable<String> fields, final Pe
117114
try {
118115
return collection.ensurePersistentIndex(fields, options);
119116
} catch (final ArangoDBException e) {
120-
throw translateExceptionIfPossible(e);
117+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
121118
}
122119
}
123120

@@ -127,7 +124,7 @@ public IndexEntity ensureGeoIndex(final Iterable<String> fields, final GeoIndexO
127124
try {
128125
return collection.ensureGeoIndex(fields, options);
129126
} catch (final ArangoDBEx 10000 ception e) {
130-
throw translateExceptionIfPossible(e);
127+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
131128
}
132129
}
133130

@@ -138,7 +135,7 @@ public IndexEntity ensureFulltextIndex(final Iterable<String> fields, final Full
138135
try {
139136
return collection.ensureFulltextIndex(fields, options);
140137
} catch (final ArangoDBException e) {
141-
throw translateExceptionIfPossible(e);
138+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
142139
}
143140
}
144141

@@ -147,7 +144,7 @@ public IndexEntity ensureTtlIndex(Iterable<String> fields, TtlIndexOptions optio
147144
try {
148145
return collection.ensureTtlIndex(fields, options);
149146
} catch (final ArangoDBException e) {
150-
throw translateExceptionIfPossible(e);
147+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
151148
}
152149
}
153150

@@ -156,7 +153,7 @@ public void dropIndex(final String id) throws DataAccessException {
156153
try {
157154
collection.deleteIndex(id);
158155
} catch (final ArangoDBException e) {
159-
throw translateExceptionIfPossible(e);
156+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
160157
}
161158
}
162159

@@ -165,7 +162,7 @@ public void grantAccess(final String username, final Permissions permissions) {
165162
try {
166163
collection.grantAccess(username, permissions);
167164
} catch (final ArangoDBException e) {
168-
throw translateExceptionIfPossible(e);
165+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
169166
}
170167
}
171168

@@ -174,7 +171,7 @@ public void resetAccess(final String username) {
174171
try {
175172
collection.resetAccess(username);
176173
} catch (final ArangoDBException e) {
177-
throw translateExceptionIfPossible(e);
174+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
178175
}
179176
}
180177

@@ -183,7 +180,7 @@ public Permissions getPermissions(final String username) throws DataAccessExcept
183180
try {
184181
return collection.getPermissions(username);
185182
} catch (final ArangoDBException e) {
186-
throw translateExceptionIfPossible(e);
183+
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
187184
}
188185
}
189186

0 commit comments

Comments
 (0)
0