burawi / tayr

an easy on-the-fly ORM for NodeJS, inspired by RedBeanPHP
http://burawi.github.io/tayr/
MIT License
20 stars 2 forks source link

Error handling #2

Open zumoshi opened 6 years ago

zumoshi commented 6 years ago

hi,

I really like redbeanphp, and finding your library made me excited, however before I can use it, I see a few problems.

first one is I don't see any way to handle errors. The most basic example being:

var nest = require('Tayr');
var T = new nest({
    host: 'localhost',
    database: 'databaseName'
});

if there is no MySQL server running on localhost, connection.connect(); will get ECONNREFUSED, however, there is no callback given to it, to get the error and reject the promise.

sequelize for example lets you handle connections errors this way:

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

maybe we can expose the createDb function instead of executing it automatically, and allow the user to catch connection errors that way ?

var nest = require('Tayr');
var T = new nest({
    host: 'localhost',
    database: 'databaseName'
}); // createDb is not called automatically, so no exception is raised

T.createDb().then(()=>{
// database is ready
}).catch((err)=>{
//err can be ECONNREFUSED
})

another example is on line 497, if (err) throw err; will either crash the node process or keep the promise in pending state indefinitely.

Instead, something like if (err) return reject(err); would be more appropriate. The goal here is to allow catching and handling of errors in user code.

I also think that the onReject method is not very helpful, instead, I would prefer if errors would be handlable using a .catch in user code.

burawi commented 6 years ago

Sorry for the very late answer! A big thanks for your comment I will work on it soon! I promise you