added error handling in database.js