I am creating an app that will auth user input. problem is that when the user input is wrong error is not caught and my app is crashing.
If i put
throw new Error("dasdas");
That error is caught but IMAP errors are not.
try {
var imap = new Imap({
user: email,
password: password,
host: "mail.metropolitan.ac.rs",
port: 993,
tls: true,
});
imap.connect();
res.status(201).json({ success: true, person: email });
} catch (err) {
console.log(err);
return res.status(400).json({ success: false, msg: "data is wrong" });
}
As shown in the documentation you need to register the error listener before connecting to the server like this as the error is emitted as an event and not thrown as a normal error would be
imap.once('error', function(err) {
console.log(err);
});
(This needs to be done before connect()) you can read about this behaviour on this page