2

I'm relatively inexperienced with Node and I've been asked to create a POP3 email client. The newest package I could find that handles POP3 is node-poplib-yapc and I've been playing around with it, seeing how it works.

Now the problem I've run into is that I don't know how to do error handling with this package, I've been purposefully giving it the wrong login details so that I can test it's error handling. When I give it the correct credentials it seems to work fine (I've been able to login and download emails) so I doubt there's an issue with the package or how I call the package. The docs is also completely unclear about it, the only thing that I could find related to (what I think is) error handling says the following:

connect(callback)

  • callback - function(err) // <- This 'err' variable is what I thought meant 'error'

Connect to the mailserver using hostname and port. Starts TLS connection if tls property is true. Then login into your mailbox using credentials properties username and password.

After reading the above I tried the following:

let popClient = require('node-poplib-yapc').Client;
let client = new popClient({ /* my incorrect credentials */ });

client.connect((err) => {
    if(err) {
        console.log('There was an error');
        console.log(err);
    }
});

But this doesn't work, instead it's throwing an actual error in the terminal (I'll add the output below). I also tried the following with the same results:

client.connect(() => {
}, (err) => {
    console.log('There was an error');
    console.log(err);
});

And the good old try/catch, which also let me down:

try {
  client.connect(() => {
  });
} catch (err) {
  console.log(err);
}

Here is the error it throws in the terminal when I run any of the above code:

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

Error: [AUTH] Username and password not accepted.
    at Client.onData (/home/lee/github/chatterbox/node_modules/node-poplib-yapc/main.js:97:10)
    at TLSSocket.emit (events.js:180:13)
    at addChunk (_stream_readable.js:269:12)
    at readableAddChunk (_stream_readable.js:256:11)

    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onread (net.js:578:20)
Emitted 'error' event at:
    at Client.onData (/home/lee/github/chatterbox/node_modules/node-poplib-yapc/main.js:107:10)
    at TLSSocket.emit (events.js:180:13)
[... lines matching original stack trace ...]
    at TLSWrap.onread (net.js:578:20)

The last thing I also tried is this:

client.connect(() => {
}).catch(err => {
    console.log('There was an error');
    console.log(err);
});

Which resulted in the following error:

/home/lee/github/chatterbox/service/email.js:29
  }).catch(err => {
    ^

TypeError: Cannot read property 'catch' of undefined
    at Object.getMail (/home/lee/github/chatterbox/service/email.js:29:5)
    at accounts.forEach (/home/lee/github/chatterbox/app.js:17:11)
    at Array.forEach (<anonymous>)

    at main (/home/lee/github/chatterbox/app.js:16:12)
    at Object.<anonymous> (/home/lee/github/chatterbox/app.js:27:1)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)

If anyone could please help me out with how to properly handle errors with packages like this one, I'd be most appreciative. Obviously a solution that handles any and all errors (not just authentication errors) is preferred, but really just anything that points me in the right direction would help a lot.

3 Answers 3

2

The reason client throws an error is that it's an EventEmitter and it emits an 'error' event. Since you don't have any event listeners on the 'error' event it blows up.

This is what you're missing.

client.on('error', err => {
  console.error('client got error', err)
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! D'ya by any chance know if there's a way to close/exit/return from the client() function in the case of an error? From the docs I learned that client.quit() can be used inside client.connect() to exit it but that doesn't seem to be the case for client.on('error', ...) and so what's happening now is that my error handling function will run as it should but then it will immediately carry on inside client.connect() printing my console.log()s and never actually exit or return anything.
0

Actually first try is OK. When you call connect, if there is error in credentials, callback is called with first param err, so only thing that you are missing is return in if block.

let popClient = require('node-poplib-yapc').Client;
let client = new popClient({ /* my incorrect credentials */ });

client.connect((err) => {
    if(err) {
        console.log('There was an error');
        console.log(err);
        ... you can throw additional error and what you need but after must
        return;
    }
    ... if there is no error you call list retrieve or what you need
});

Comments

0

Well for me, I needed both error callback handler in connect method as well as separate error event handler.

client.on('error', err => {
  console.error('client got error', err)
})
client.connect(function(err) {
  if(err)
  {
    console.log('error');
  }
  else{

    console.log('success');
  }

  })

 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.