postalsys / imapflow

IMAP Client library for EmailEngine Email API (https://emailengine.app)
https://imapflow.com
Other
350 stars 59 forks source link

Unexpected behavior while fetch if network error occurs #201

Closed udik-chudik closed 3 months ago

udik-chudik commented 3 months ago

There is an unexpected behavior during some kind of errors. This make the usage of such a beautiful lib unreliable

To Reproduce

The code from README: while fetching is in progress

await (let message of client.fetch('1:*', { envelope: true })) 

and network error occurs (for example turn wifi off/on), the program crashes with

      throw er; // Unhandled 'error' event

After adding a listener for an error event, for example:

client.on('error', console.log)

crashes stopped. But none of .catch are fired: try in 'main' neither main().catch. main().then does not fires as well.

Expected behavior Expect

await (let message of client.fetch('1:*', { envelope: true })) 

to throw if a network error occurs.

Additional context Node v20.14.0

andris9 commented 3 months ago

fetch and all other commands throw on IMAP response failures - these are errors you can recover from in the same IMAP session. For network etc failures that break the IMAP session, you need to set up the generic error handler, and when it fires, destroy the existing ImapFlow object and create and connect a new one.