Handling connection error to beanstalk
Firzenizer opened this issue · 4 comments
This is part of my code and it seems to raise an error when I try to run it when Beanstalk is not running.
var bs = require('nodestalker'),
client = bs.Client(beanstalkdHost),
tube = 'my-tube';
client.watch(tube).onSuccess(function (data) {
// DO STUFF
)};
Error:
events.js:85
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect as oncomplete
What I'd like to do is catch this error and retry in 5 seconds to connect and see if Beanstalk is responding. Right now I can't come up with a way to catch this error. And how can I see, if client is connected?
Hmm, without having tried it out, I'd do an init function with a try catch or an error handler if async, and if the error is ECONNREFUSED do a setInterval that call the init again?
This client.watch is already in a try-catch. I also had a init function to call inside interval, but it's no use if I can't catch the error. I have tried adding try-catches all over the code. It just won't catch anything.
I guess it is one of nodestalker's dependencies that throw this error.
Been a while since I looked at this code.
Okay, the key is in this line: https://github.com/pascalopitz/nodestalker/blob/master/lib/beanstalk_client.js#L330
The error gets re-emitted on the client object. All you will need to do is a client.on('error', function () {}); in order to catch it. If not handled, the error will bubble.
Oh. I totally missed it. I was browsing the source for hours. This totally saved my work day. Thanks!