2

Given:

  • NodeJS v0.10.25
  • All Harmony features enabled
  • "use strict"

And the following code:

   db.connect({
      host: DB_HOST,
      port: DB_PORT
    }).then(function(dbConn) {
      console.log('DBASE connected to ' + DB_HOST + ':' + DB_PORT);
      db.dbList().run(dbConn).then(function(result) {
        if (result.indexOf(SCRIPT_NAME) == -1) throw new Error('unable to locate database ' + SCRIPT_NAME);
        dbConn.use(SCRIPT_NAME);
        console.log('DBASE bound to ' + SCRIPT_NAME + ' on ' + DB_HOST + ':' + DB_PORT);
        db.tableList().run(dbConn)
          .then(function(result) {
            if (!result) throw new Error(SCRIPT_NAME + ' unable to enumerate tables');
            if (!result.length) throw new Error(SCRIPT_NAME + ' has no tables');
            console.log('DBASE ' + DB_HOST + ':' + DB_PORT + '/' + SCRIPT_NAME + ' has ' + result.length + ' table' + ((result.length > 1) ? 's' : ''));
          }).catch(function(err) {
            console.error('DBASE ' + err);
          });
      }).catch(function(err) {
        console.error('DBASE ' + err);
      });
    }).catch(function(err) {
      console.error('DBASE ' + err);
    });

Note the multiple identical catch blocks:

.catch(function(err) {
          console.error('DBASE ' + err);
        });

Is there a recommended / accepted / de facto way to reuse that exception handler across multiple levels of control structures?

2 Answers2

5

The errors will bubble until they're caught so you don't need multiple catches and you can make your code more readable by chaining your promises instead of nesting them:

db.connect({
  host: DB_HOST,
  port: DB_PORT
}).then(function(dbConn) {
  console.log('DBASE connected to ' + DB_HOST + ':' + DB_PORT);
  // it's important to return if you have a promise so the chain doesn't break
  return db.dbList().run(dbConn);
}).then(function(result) {
  if (result.indexOf(SCRIPT_NAME) == -1) throw new Error('unable to locate database ' + SCRIPT_NAME);
  dbConn.use(SCRIPT_NAME);
  console.log('DBASE bound to ' + SCRIPT_NAME + ' on ' + DB_HOST + ':' + DB_PORT);
  return db.tableList().run(dbConn);
}).then(function(result) {
  if (!result) throw new Error(SCRIPT_NAME + ' unable to enumerate tables');
  if (!result.length) throw new Error(SCRIPT_NAME + ' has no tables');
  console.log('DBASE ' + DB_HOST + ':' + DB_PORT + '/' + SCRIPT_NAME + ' has ' + result.length + ' table' + ((result.length > 1) ? 's' : ''));
}).catch(function(err) {
  console.error('DBASE ' + err);
});
Shanoor
  • 13,344
  • 2
  • 29
  • 40
0

ShanShan's answer is correct, but since you said you're using ES2015 on modern Node you can use more modern syntax:

db.connect({
  host: DB_HOST,
  port: DB_PORT
}).then(dbConn => {
  console.log(`DBASE connected to ${DB_HOST} : ${DB_PORT}`);
  return db.dbList().run(dbConn);
}).then(result =>{
  if (!result.includes(SCRIPT_NAME)) 
    throw new Error(`unable to locate database ${SCRIPT_NAME}`);
  dbConn.use(SCRIPT_NAME);
  console.log(`DBASE bound to ${SCRIPT_NAME} on ${DB_HOST} : ${DB_PORT}`);
  return db.tableList().run(dbConn);
}).then(result => 
  if (!result) throw new Error(`${SCRIPT_NAME} unable to enumerate tables`);
  if (!result.length)  throw new Error(`${SCRIPT_NAME has no tables`);
  console.log(`DBASE ${DB_HOST} : ${DB_PORT}/${SCRIPT_NAME} has ` + `
              `${result.length}  table ${((result.length > 1) ? 's' : '')}`);

process.on('unhandledRejection', (p) => console.error('DBASE', p)); // global handler

Even further, if we use a generator pump, the code can be written as:

function co(gen) { // your friendly neighborhood generator pump
   var it = gen(); 
   return Promise.resolve().then(function next(v){ 
      let r = it.next(v);
      if(r.done) return r.value;
      return Promise.resolve(r.value).then(next, e => it.throw(e));
   });
}

co(function*() { // console logs ommitted for brevity
  const db = yield db.connect({host: DB_HOST, port: DB_PORT }); 
  const list = yield db.dbList().run(dbConn);
  if(!result.includes(SCRIPT_NAME))
    throw new Error(`unable to locate database ${SCRIPT_NAME}`);
  db.use(SCRIPT_NAME);
  const tables = db.tableList().run(db);
  if(!tables) throw new Error(`${SCRIPT_NAME} unable to enumerate tables`);
  if(!tables.length) throw new Error(`${SCRIPT_NAME} has no tables`);
  return tables; // this still returns a promise, you can chain off it
});

And that's it, completely flat, you can use synchronous try/catch there and it'd work or also use .catch on the resulting promise if you prefer.

You can use a library like bluebird for coroutines instead of the above co function which would give you better stack traces and faster promises.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504