Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EmailReadImap Node): Improve error handling #3465

Merged
merged 1 commit into from
Jun 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ export class EmailReadImap implements INodeType {
const staticData = this.getWorkflowStaticData('node');
Logger.debug('Loaded static data for node "EmailReadImap"', {staticData});

let connection: ImapSimple;

// Returns the email text
const getText = async (parts: any[], message: Message, subtype: string) => { // tslint:disable-line:no-any
if (!message.attributes.struct) {
Expand Down Expand Up @@ -440,19 +442,29 @@ export class EmailReadImap implements INodeType {
// Connect to the IMAP server and open the mailbox
// that we get informed whenever a new email arrives
return imapConnect(config).then(async conn => {
conn.on('error', async err => {
if (err.code.toUpperCase() === 'ECONNRESET') {
Logger.verbose('IMAP connection was reset - reconnecting.');
connection = await establishConnection();
await connection.openBox(mailbox);
conn.on('error', async error => {
const errorCode = error.code.toUpperCase();
if (['ECONNRESET', 'EPIPE'].includes(errorCode)) {
Logger.verbose(`IMAP connection was reset (${errorCode}) - reconnecting.`, { error });
try {
connection = await establishConnection();
await connection.openBox(mailbox);
return;
} catch (e) {
Logger.error('IMAP reconnect did fail', { error: e });
// If something goes wrong we want to run emitError
}
} else {
Logger.error('Email Read Imap node encountered a connection error', { error });
}
throw err;

this.emitError(error);
});
return conn;
});
};

let connection: ImapSimple = await establishConnection();
connection = await establishConnection();

await connection.openBox(mailbox);

Expand Down