From 846e866dafc74911466b276f5cefc58274e8ce02 Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Sat, 19 Mar 2022 20:03:58 +0100 Subject: [PATCH] bug(EmailReadImap Node): Improve error handling (#2991) * Fix: EmailReadImap unhandled promise rejection Related to #2091 (but only partially) See https://github.com/n8n-io/n8n/issues/2091#issuecomment-980010289 * Send errors from email read imap to logger Co-authored-by: Manuel [tennox] <2084639+tennox@users.noreply.github.com> --- .../nodes/EmailReadImap/EmailReadImap.node.ts | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts b/packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts index 4b4f2e43769..3cd39dfb9bf 100644 --- a/packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts +++ b/packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts @@ -7,6 +7,7 @@ import { INodeType, INodeTypeDescription, ITriggerResponse, + LoggerProxy, NodeOperationError, } from 'n8n-workflow'; @@ -377,6 +378,18 @@ export class EmailReadImap implements INodeType { }; const establishConnection = (): Promise => { + + let searchCriteria = [ + 'UNSEEN', + ] as Array; + if (options.customEmailConfig !== undefined) { + try { + searchCriteria = JSON.parse(options.customEmailConfig as string); + } catch (error) { + throw new NodeOperationError(this.getNode(), `Custom email config is not valid JSON.`); + } + } + const config: ImapSimpleOptions = { imap: { user: credentials.user as string, @@ -388,16 +401,6 @@ export class EmailReadImap implements INodeType { }, onmail: async () => { if (connection) { - let searchCriteria = [ - 'UNSEEN', - ] as Array; - if (options.customEmailConfig !== undefined) { - try { - searchCriteria = JSON.parse(options.customEmailConfig as string); - } catch (error) { - throw new NodeOperationError(this.getNode(), `Custom email config is not valid JSON.`); - } - } if (staticData.lastMessageUid !== undefined) { searchCriteria.push(['UID', `${staticData.lastMessageUid as number}:*`]); /** @@ -415,10 +418,14 @@ export class EmailReadImap implements INodeType { Logger.debug('Querying for new messages on node "EmailReadImap"', {searchCriteria}); } - const returnData = await getNewEmails(connection, searchCriteria); - - if (returnData.length) { - this.emit([returnData]); + try { + const returnData = await getNewEmails(connection, searchCriteria); + if (returnData.length) { + this.emit([returnData]); + } + } catch (error) { + Logger.error('Email Read Imap node encountered an error fetching new emails', { error }); + throw error; } } },