improved error handling in db.js