emailjs / emailjs-imap-client

Low-level JS IMAP client for all your IMAP needs.
MIT License
553 stars 122 forks source link

UnhandledPromiseRejectionWarning with incorrect credentials #175

Closed CodeCook83 closed 6 years ago

CodeCook83 commented 6 years ago

Hi,

I get an UnhandledPromiseRejectionWarning in the console when I try to test the connection with an incorrect password. Both tests with correct and incorrect auth data, pass through the .then() function of the client.connect().

How can I tell the user that his credentials are wrong? Maybe over the .catch() function and then over the $.get().fail()? How can I catch this information?

My request comes via $.get from the view to app.js. After that it passes to my controller, where I process a method to validate the connection data.

Thanks a lot

Plattform: OS: dev: macOS High Sierra 10.13.3; prod: heroku Node: 9.4.0 npm: 5.6.0 jQuery: 3.3.1

The warning (or error?)

[DEBUG][2018-02-12T20:13:56.463Z][1][testmail1234@arcor.de][imap.arcor.de] S: W3 BAD Error in IMAP command received by server.
(node:39422) UnhandledPromiseRejectionWarning: Error: Error in IMAP command received by server.
    at Object.callback (/Users/kt/Development/mailtool/node_modules/emailjs-imap-client/dist/imap.js:289:25)
    at Imap._handleResponse (/Users/kt/Development/mailtool/node_modules/emailjs-imap-client/dist/imap.js:621:28)
    at Imap._parseIncomingCommands (/Users/kt/Development/mailtool/node_modules/emailjs-imap-client/dist/imap.js:585:12)
    at Imap._onData (/Users/kt/Development/mailtool/node_modules/emailjs-imap-client/dist/imap.js:428:10)
    at TCPSocket.socket.ondata.evt [as ondata] (/Users/kt/Development/mailtool/node_modules/emailjs-imap-client/dist/imap.js:160:16)
    at TCPSocket._emit (/Users/kt/Development/mailtool/node_modules/emailjs-tcp-socket/dist/node-socket.js:101:31)
    at TLSSocket.<anonymous> (/Users/kt/Development/mailtool/node_modules/emailjs-tcp-socket/dist/node-socket.js:67:23)
    at TLSSocket.emit (events.js:160: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:599:20)
(node:39422) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

main.js (client)

...
$('.connTest').click(function (e) {
    e.preventDefault();
    var data = $('.old').find('select, input').serialize();
    $.get('/testconn', data, function (data) {
      $('.connTest').removeClass('btn-warning').
      addClass('btn-success').text(" " + data).prepend("<i class='fas fa-check'></i>");
    }).fail(function () {
      alert('error'); // anything
    });
  })
...

router.js

...
router.get('/testconn', controller.testConnGet);
...

controller.js

...
exports.testConnGet = function (req, res) {
  const provider = req.query.provider;
  const serviceIsInList = _.findKey(services, (value, key) => {
    return key.indexOf(req.query.provider) >= 0;
  })
  if(serviceIsInList) {
    const client = new ImapClient(
      services[provider].host, 
      services[provider].port, {
      auth: {
        user: services[provider].auth.user,
        pass: services[provider].auth.password
      }
    });
    client.onerror = function(error){
      console.log(error);
    }
    client.connect()
      .then(() => {
        res.send('Connection Ok');
      })
      .catch(() => {
        res.send('Bad connection!')
      })
  }
}
...
felixhammerl commented 6 years ago

Thanks for reporting it. It also shows up in a test case and I've been meaning to investigate it for a while. Will look into that later today.

CodeCook83 commented 6 years ago

Thank you for taking care of my report. Hope one day I'll be able to fix something like that myself :-)

felixhammerl commented 6 years ago

Sorry for taking so long on this, I had my hands full recently as i did a bunch of business travels and workshops, so i really didn't have the capacity to clean this up earlier. After bashing my head against the wall for a while, i found the issue: a missing await. head -> desk. thanks for the patience :)

greetings from nullcon at goa, india.

CodeCook83 commented 6 years ago

Hi @felixhammerl it worked great. I treated the error handling as follows:

https://github.com/CodeCook83/offhaul/blob/c6e797c00b2072326e5c4d746e1e66ada237315c/public/js/main.js#L104

Could you tell me if my way is alright or if I forgot to insert a "message"?