ArangoDB Transactions - How to Prevent Exception Exception

How to prevent ArangoDB from throwing an exception during a transaction when looking for a specific document that may not exist at this point?

Nodejs sends the transaction in one block to ArangoDb, where it is processed. It's fine. I want to upload all the math to the server.

During the transaction, I want to look at a specific collection and check if the document exists, if the document can be found, and then get the "balance" field, but if the document is not found or its field, then I do not want to throw an exception and do not want to stop the current transaction. On the contrary, I want to continue the transaction much more, and we will assign the string "0" to the oldBalance variable.

(for your information: there is a write lock for the collection: "user" is indicated on the nodeJS side) and here you see part of the transaction code sent to ArangoDB:

var db = require('internal').db;
// 1.) find specific document
var accountdoc = db.user.document('Johnny04'); // find doc by _key

this throws an exception if this document is not found. At that time, the user probably did not have an entry in the collection. In this case, we want to consider its balance line '0'. But, unfortunately, an exception has already been thrown. I would much like to do the following:

//2.) calculate newBalance = oldBalance + additional
        if (accountdoc.error==true){ // document not found etc...
            var oldBalance='0';
            var documentExists = false;
        } else {
            var oldBalance=accountdoc.balance;
            var documentExists = true;
            var documentExistsID = accountdoc._id;
        }   
+4
source share
1 answer

It is not possible to handle the "document not found" error inside a transaction as follows:

function (params) {
  var db = require("org/arangodb").db;
  var accountdoc;

  // 1.) find specific document
  try {
    accountdoc = db.user.document('Johnny04'); // find doc by _key
  }
  catch (err) {
    // document not found etc.
    // TODO: rethrow exception if err is something different than "document not found"
  }

  // 2.) calculate newBalance = oldBalance + additional
  if (accountdoc === undefined) { // document not found etc...
    // create a new document with balance 0
    db.user.save({ _key: 'Johnny04', balance: '0' }); // note: if this fails, the transaction will throw
  } 
  else {
    // update the existing document
    var oldBalance = accountdoc.balance;
    var newBalance = oldBalance + 42;
    db.user.update('Johnny04', { balance: newBalance }); // note: if this fails, the transaction will throw
  }   
}
+4
source

Source: https://habr.com/ru/post/1542485/


All Articles