Skip to content

Commit

Permalink
arangodb#250 implement and use exception translator as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
aburmeis committed Aug 2, 2023
1 parent 250bfad commit 69c03f8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.springframework.context.expression.BeanFactoryAccessor;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.PersistentPropertyAccessor;
Expand Down Expand Up @@ -161,10 +162,6 @@ private ArangoDatabase db() {
});
}

private DataAccessException translateExceptionIfPossible(final RuntimeException exception) {
return exceptionTranslator.translateExceptionIfPossible(exception);
}

private ArangoCollection _collection(final String name) {
return _collection(name, null, null);
}
Expand Down Expand Up @@ -337,7 +334,7 @@ public ArangoDBVersion getVersion() throws DataAccessException {
}
return version;
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand Down Expand Up @@ -386,7 +383,7 @@ public MultiDocumentEntity<? extends DocumentEntity> delete(final Iterable<Objec
try {
result = _collection(entityClass).deleteDocuments(toVPackCollection(values), entityClass, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

potentiallyEmitAfterDeleteEvent(values, entityClass, result);
Expand All @@ -409,7 +406,7 @@ public DocumentEntity delete(final Object id, final Class<?> entityClass, final
try {
result = _collection(entityClass, id).deleteDocument(determineDocumentKeyFromId(id), entityClass, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

potentiallyEmitEvent(new AfterDeleteEvent<>(id, entityClass));
Expand All @@ -431,7 +428,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> update(final Iterable<T
try {
result = _collection(entityClass).updateDocuments(toVPackCollection(values), options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFields(values, result);
Expand All @@ -456,7 +453,7 @@ public DocumentEntity update(final Object id, final Object value, final Document
result = _collection(value.getClass(), id).updateDocument(determineDocumentKeyFromId(id), toVPack(value),
options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFields(value, result);
Expand All @@ -479,7 +476,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> replace(final Iterable<
try {
result = _collection(entityClass).replaceDocuments(toVPackCollection(values), options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFields(values, result);
Expand All @@ -503,7 +500,7 @@ public DocumentEntity replace(final Object id, final Object value, final Documen
result = _collection(value.getClass(), id).replaceDocument(determineDocumentKeyFromId(id), toVPack(value),
options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFields(value, result);
Expand All @@ -524,7 +521,7 @@ public <T> Optional<T> find(final Object id, final Class<T> entityClass, final D
VPackSlice.class, options);
return Optional.ofNullable(fromVPack(entityClass, doc));
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -549,7 +546,7 @@ public <T> Iterable<T> find(final Iterable<? extends Object> ids, final Class<T>
final MultiDocumentEntity<VPackSlice> docs = _collection(entityClass).getDocuments(keys, VPackSlice.class);
return docs.getDocuments().stream().map(doc -> fromVPack(entityClass, doc)).collect(Collectors.toList());
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -563,7 +560,7 @@ public <T> MultiDocumentEntity<? extends DocumentEntity> insert(final Iterable<T
try {
result = _collection(entityClass).insertDocuments(toVPackCollection(values), options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFields(values, result);
Expand All @@ -585,7 +582,7 @@ public DocumentEntity insert(final Object value, final DocumentCreateOptions opt
try {
result = _collection(value.getClass()).insertDocument(toVPack(value), options);
} catch (final ArangoDBException e) {
throw exceptionTranslator.translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFields(value, result);
Expand All @@ -607,7 +604,7 @@ public DocumentEntity insert(final String collectionName, final Object value, fi
try {
result = _collection(collectionName).insertDocument(toVPack(value), options);
} catch (final ArangoDBException e) {
throw exceptionTranslator.translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFields(value, result);
Expand Down Expand Up @@ -706,7 +703,7 @@ public <T> void repsert(final T value) throws DataAccessException {
clazz
).first();
} catch (final ArangoDBException e) {
throw exceptionTranslator.translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFieldsFromObject(value, result);
Expand All @@ -733,7 +730,7 @@ public <T> void repsert(final Iterable<? extends T> values, final Class<T> entit
entityClass
).asListRemaining();
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}

updateDBFieldsFromObjects(values, result);
Expand Down Expand Up @@ -813,7 +810,7 @@ public boolean exists(final Object id, final Class<?> entityClass) throws DataAc
try {
return _collection(entityClass).documentExists(determineDocumentKeyFromId(id));
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -823,7 +820,7 @@ public void dropDatabase() throws DataAccessException {
try {
db.drop();
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
databaseCache.remove(db.name());
collectionCache.keySet().stream().filter(key -> key.getDb().equals(db.name()))
Expand Down Expand Up @@ -860,7 +857,7 @@ public Iterable<UserEntity> getUsers() throws DataAccessException {
try {
return arango.getUsers();
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.dao.support.PersistenceExceptionTranslator;

import com.arangodb.ArangoCollection;
Expand Down Expand Up @@ -57,10 +58,6 @@ protected DefaultCollectionOperations(final ArangoCollection collection,
this.exceptionTranslator = exceptionTranslator;
}

private DataAccessException translateExceptionIfPossible(final RuntimeException exception) {
return exceptionTranslator.translateExceptionIfPossible(exception);
}

@Override
public String name() {
return collection.name();
Expand All @@ -72,7 +69,7 @@ public void drop() throws DataAccessException {
try {
collection.drop();
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -81,7 +78,7 @@ public void truncate() throws DataAccessException {
try {
collection.truncate();
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -91,7 +88,7 @@ public long count() throws DataAccessException {
final Long count = collection.count().getCount();
return count != null ? count.longValue() : -1;
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -100,7 +97,7 @@ public CollectionPropertiesEntity getProperties() throws DataAccessException {
try {
return collection.getProperties();
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -109,7 +106,7 @@ public Collection<IndexEntity> getIndexes() throws DataAccessException {
try {
return collection.getIndexes();
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -119,7 +116,7 @@ public IndexEntity ensureHashIndex(final Iterable<String> fields, final HashInde
try {
return collection.ensureHashIndex(fields, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -129,7 +126,7 @@ public IndexEntity ensureSkiplistIndex(final Iterable<String> fields, final Skip
try {
return collection.ensureSkiplistIndex(fields, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -139,7 +136,7 @@ public IndexEntity ensurePersistentIndex(final Iterable<String> fields, final Pe
try {
return collection.ensurePersistentIndex(fields, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -149,7 +146,7 @@ public IndexEntity ensureGeoIndex(final Iterable<String> fields, final GeoIndexO
try {
return collection.ensureGeoIndex(fields, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -159,7 +156,7 @@ public IndexEntity ensureFulltextIndex(final Iterable<String> fields, final Full
try {
return collection.ensureFulltextIndex(fields, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -168,7 +165,7 @@ public IndexEntity ensureTtlIndex(Iterable<String> fields, TtlIndexOptions optio
try {
return collection.ensureTtlIndex(fields, options);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -177,7 +174,7 @@ public void dropIndex(final String id) throws DataAccessException {
try {
collection.deleteIndex(id);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -186,7 +183,7 @@ public void grantAccess(final String username, final Permissions permissions) {
try {
collection.grantAccess(username, permissions);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -195,7 +192,7 @@ public void resetAccess(final String username) {
try {
collection.resetAccess(username);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand All @@ -204,7 +201,7 @@ public Permissions getPermissions(final String username) throws DataAccessExcept
try {
return collection.getPermissions(username);
} catch (final ArangoDBException e) {
throw translateExceptionIfPossible(e);
throw DataAccessUtils.translateIfNecessary(e, exceptionTranslator);
}
}

Expand Down
Loading

0 comments on commit 69c03f8

Please sign in to comment.