arangodb / arangodb-java-driver

The official ArangoDB Java driver.

Repository from Github https://github.comarangodb/arangodb-java-driverRepository from Github https://github.comarangodb/arangodb-java-driver

Exception is not thrown correctly

mrh1234 opened this issue · comments

When a document with a certain key does not exist in a collection, the ArangoDBException is not passed up and cannot be thrown in my code, such that you can not handle it by executing the catch statement.

When you run the following example, you will get a NullPointerException for the first System.out.println(), because the Exception is not thrown in this code in the line before (I guess it is handled deeper in the call stack).

This simple code piece from the tutorial cannot be executed when the document with the key does not exist in the collection. So the database and the collection exist, however the document with the key "myKey" exists not.

try {
  BaseDocument myUpdatedDocument = arangoDB.db(dbName).collection(collectionName).getDocument("myKey", BaseDocument.class);
  System.out.println("Key: " + myUpdatedDocument.getKey());
  System.out.println("Attribute a: " + myUpdatedDocument.getAttribute("a"));
  System.out.println("Attribute b: " + myUpdatedDocument.getAttribute("b"));
  System.out.println("Attribute c: " + myUpdatedDocument.getAttribute("c"));
} catch (ArangoDBException e) {
  System.err.println("Failed to get document: myKey; " + e.getMessage());
}

These error messages result from the code before:

com.arangodb.ArangoDBException: Response: 404, Error: 1202 - document not found
	at com.arangodb.internal.util.ResponseUtils.checkError(ResponseUtils.java:52) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.velocystream.VstCommunication.checkError(VstCommunication.java:141) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.velocystream.VstCommunicationSync.execute(VstCommunicationSync.java:130) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.velocystream.VstCommunicationSync.execute(VstCommunicationSync.java:44) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.velocystream.VstCommunication.execute(VstCommunication.java:135) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.velocystream.VstProtocol.execute(VstProtocol.java:46) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.ArangoExecutorSync.execute(ArangoExecutorSync.java:71) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.ArangoExecutorSync.execute(ArangoExecutorSync.java:57) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.ArangoExecutorSync.execute(ArangoExecutorSync.java:53) ~[arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.ArangoCollectionImpl.getDocument(ArangoCollectionImpl.java:105) [arangodb-java-driver-6.12.1.jar:?]
	at com.arangodb.internal.ArangoCollectionImpl.getDocument(ArangoCollectionImpl.java:97) [arangodb-java-driver-6.12.1.jar:?]
	at ArangoDBTest.main(ArangoDBTest.java:42) [test/:?]
Exception in thread "main" java.lang.NullPointerException
	at ArangoDBTest.main(ArangoDBTest.java:43)

Thanks for reporting, in case no document for the specified key is found, null is returned from ArangoCollection.getDocument(). I will update the tutorial page accordingly.

The tutorial page has been updated accordingly.