xpepermint / smtp-client

Simple, promisified, protocol-based SMTP client for Node.js.
36 stars 13 forks source link

Handling ECONNRESET exceptions #21

Open stefanolaru opened 1 year ago

stefanolaru commented 1 year ago

Hi,

Thank you for writing this client, it's really helpful.

Running it in an AWS Lambda (node 18) and occasionally it throws ECONNRESET. Some SMTP servers seem to terminate the connection when I attempt the MAIL command.

ERROR   Uncaught Exception  {
    "errorType": "Error",
    "errorMessage": "read ECONNRESET",
    "code": "ECONNRESET",
    "errno": -104,
    "syscall": "read",
    "stack": [
        "Error: read ECONNRESET",
        "    at TCP.onStreamRead (node:internal/stream_base_commons:217:20)"
    ]
}

For some reason I'm not able to catch the ECONNRESET, it doesn't trigger the on('error') listener, nor with try/catch.

Any idea how to handle this type of exception?

iamoskvin commented 10 months ago

Did you find a solution?

hyperair commented 8 months ago

I tracked this issue down to EventEmitter.emit('error', errorObject) turning into an unhandled rejection when there are no handlers, see below:

https://stackoverflow.com/questions/35185749/how-to-avoid-promise-rejection-when-firing-error-event-with-eventemitter-in-no

https://github.com/xpepermint/smtp-channel/blob/d191b7fa17194a704b8c7317f651aed6fc73271f/src/index.js#L341

The workaround is to do something like:

const smtpClient = new SmtpClient(...);
(smtpClient as any as EventEmitter).on('error', () => {});