0

all I am new to nodejs and I am facing this issue while executing my node file. I am on centos 7. When I am trying to connect to LDAP using LDAPJS. It works fine when LDAP is available but then it cannot connect to server it show this weird error :

events.js:160 throw er; // Unhandled 'error' event ^

Error: connect EHOSTUNREACH 172.24.130.203:389 at Object.exports._errnoException (util.js:1020:11) at exports._exceptionWithHostPort (util.js:1043:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1099:14)

I want to catch this error and show a proper error message.

I have tried using this :

 rm -rf node_modules && npm cache clean --force && npm install

But it did not help.

My package.json file is :

{
  "name": "package",
  "version": "1.0.0",
  "description": "For LDAP ",
  "main": "index.js",
  "dependencies": {
    "ldap": "^0.7.1",
    "passport": "^0.4.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "node index.js"
  },
  "keywords": [
    "ldapjs"
  ],
  "author": "Rajan",
  "license": "ISC"
}

And this is index.js file :

var ldap = require('ldapjs');
var http = require('http');
var assert = require('assert');


var client = ldap.createClient({
    url: 'ldap://172.24.130.203/dc=india,dc=bizrtc,dc=com'
});

var basedn = "dc=test,dc=test,dc=test";

var opts = {
  filter: '(ipPhone=*)',
  scope: 'sub',
  attributes: ['ipPhone','mail','name','description']
};

client.bind('username', 'mysecret', function (err) {

client.search('dc=india,dc=bizrtc,dc=com', opts, function (err, search) {
    search.on('searchEntry', function (entry) {
      var user = entry.object;
      console.log("Users are " + user.ipPhone + "  Display Name is  " + user.name + " E-Mail is " + user.mail + " Password is  " + user.description);

    });

  });
});


client.unbind(function(err) {
  assert.ifError(err);
});
Rajan
  • 2,427
  • 10
  • 51
  • 111
  • Show your LDAP connect code. The error says `Unhandled 'error' event`, so that's all you need to do: handle the error. How is cleaning the module cache going to help? You need to write some kind of `catch` or `.on("error")` code. –  Jun 28 '19 at 07:04
  • @ChrisG I have added my code in question – Rajan Jun 28 '19 at 07:12
  • @ChrisG - yes exactly but i don't understand why it says error in some `events.js:160 throw er` – Rajan Jun 28 '19 at 07:12
  • 1
    It got inherited from events https://nodejs.org/api/events.html – Estradiaz Jun 28 '19 at 07:21

1 Answers1

1

As it seems, the client catchs socket errors and re-emits these: Client - error

So you should be fine with something like

client.on('error', err => { console.error(err) })
JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • That still does not catch the error - i don't know why – Rajan Jun 28 '19 at 11:43
  • I tried it - for me the error is caught. Where do you call client.on(...)? (please add that in your source). BTW, your package.json has a dependency on "ldap", but you require "ldapjs". These appear to be two different packages, although they might look, or in fact are, compatible. I wonder how your index.js can run after you replaced node_modules. – JeffRSon Jun 28 '19 at 12:16