Skip to content

Commit

Permalink
bug(EmailReadImap Node): Improve error handling (#2991)
Browse files Browse the repository at this point in the history
* Fix: EmailReadImap unhandled promise rejection

Related to #2091 (but only partially)

See #2091 (comment)

* Send errors from email read imap to logger

Co-authored-by: Manuel [tennox] <2084639+tennox@users.noreply.github.com>
  • Loading branch information
krynble and tennox authored Mar 19, 2022
1 parent 1b993e4 commit 846e866
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/nodes-base/nodes/EmailReadImap/EmailReadImap.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
INodeType,
INodeTypeDescription,
ITriggerResponse,
LoggerProxy,
NodeOperationError,
} from 'n8n-workflow';

Expand Down Expand Up @@ -377,6 +378,18 @@ export class EmailReadImap implements INodeType {
};

const establishConnection = (): Promise<ImapSimple> => {

let searchCriteria = [
'UNSEEN',
] as Array<string | string[]>;
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,
Expand All @@ -388,16 +401,6 @@ export class EmailReadImap implements INodeType {
},
onmail: async () => {
if (connection) {
let searchCriteria = [
'UNSEEN',
] as Array<string | string[]>;
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}:*`]);
/**
Expand All @@ -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;
}
}
},
Expand Down

0 comments on commit 846e866

Please sign in to comment.