Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
Node.js is not catching imap error, how to catch it?

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" });
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!