From 79f3625f61a0370b554fd7f72342aadfc73ad7c5 Mon Sep 17 00:00:00 2001 From: Matthew Nakama Date: Fri, 28 Apr 2017 11:42:11 -0400 Subject: [PATCH] handle Client.emit('error') events This prevents node crashes from Client.bind() and etc. We set a callback handler in the instance during bind() and send errors to it. --- lib/client/client.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/client/client.js b/lib/client/client.js index d04861b..317c74d 100644 --- a/lib/client/client.js +++ b/lib/client/client.js @@ -120,6 +120,10 @@ function Client (options) { this.log = options.log.child({ clazz: 'Client' }, true) + this.on('error', function(err) { + self.cb(err); + }) + this.timeout = parseInt((options.timeout || 0), 10) this.connectTimeout = parseInt((options.connectTimeout || 0), 10) this.idleTimeout = parseInt((options.idleTimeout || 0), 10) @@ -168,6 +172,18 @@ function Client (options) { util.inherits(Client, EventEmitter) module.exports = Client +/** + * Default handler for error callbacks when + * one isn't set in the instance + */ +Client.prototype.cb = function(err, ret) { + if (err) { + console.error('Caught exception:', err) + } else { + console.log('Unhandled output:', ret) + } +} + /** * Sends an abandon request to the LDAP server. * @@ -282,6 +298,12 @@ Client.prototype.bind = function bind (name, controls: controls }) + var self = this + this.cb = function(err, ret) { + delete self.cb + callback(err, ret) + } + return this._send(req, [errors.LDAP_SUCCESS], null, callback, _bypass) }