mxriverlynn / rabbus

A micro-service bus with built-in messaging patterns, for NodeJS and RabbitMQ
116 stars 26 forks source link

Callback or promise for error handling on request #8

Closed gideonairex closed 8 years ago

gideonairex commented 9 years ago

Hi Is there a way that this could be a callback or make them both promise objects?https://github.com/derickbailey/rabbus/blob/master/rabbus/lib/req-res/requester.js#L48-L50 The problem is that if Im going to use this as request and when there is an error its difficult to handle the error.

       rabbit
          .request(exchange.name, properties)
          .then(function(reply){
            cb(null, reply.body);
            reply.ack();
          })
          .then(null, function(err){
            cb( err );
          });

or wrapped it with promise so that we can chain the result.

       rabbit
          .request(exchange.name, properties)
          .then(function(reply){
            promise.resolve( reply.body );
            reply.ack();
          })
          .then(null, function(err){
            promise.reject( err );
          });
mxriverlynn commented 9 years ago

i agree the current set up is bad. i think the cb(err, reply.body) version would be better to do. i tried to make this change quite a while ago, but didn't have time and forgot about it.

this would be breaking change for the API of rabbus, but i think it would be a good change to make.

gideonairex commented 9 years ago

I can have a PR for this.

mxriverlynn commented 8 years ago

FWIW, I've just released v0.7.0 of Rabbus which includes a complete rewrite of the middleware and error handling processes.

Now, error are handled as middleware and include the message, properties and actions you can perform on the message:

For example:

someReceiver.use(function(err, message, props, actions, next){
  console.log(err.stack);
  actions.reject();
  next();
});

Be sure to check out the UPGRADING.md doc for more information on the changes