diff --git a/packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts b/packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts index 4b4f2e4376..3cd39dfb9b 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; } } },