Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling #2

Open
zumoshi opened this issue Sep 26, 2017 · 1 comment
Open

Error handling #2

zumoshi opened this issue Sep 26, 2017 · 1 comment

Comments

@zumoshi
Copy link

zumoshi commented Sep 26, 2017

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
Copy link
Owner

burawi commented Dec 24, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants