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.