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>
This commit is contained in:
Omar Ajoue 2022-03-19 20:03:58 +01:00 committed by GitHub
parent 1b993e4022
commit 846e866daf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,7 @@ import {
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
ITriggerResponse, ITriggerResponse,
LoggerProxy,
NodeOperationError, NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -377,6 +378,18 @@ export class EmailReadImap implements INodeType {
}; };
const establishConnection = (): Promise<ImapSimple> => { 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 = { const config: ImapSimpleOptions = {
imap: { imap: {
user: credentials.user as string, user: credentials.user as string,
@ -388,16 +401,6 @@ export class EmailReadImap implements INodeType {
}, },
onmail: async () => { onmail: async () => {
if (connection) { 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) { if (staticData.lastMessageUid !== undefined) {
searchCriteria.push(['UID', `${staticData.lastMessageUid as number}:*`]); 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}); Logger.debug('Querying for new messages on node "EmailReadImap"', {searchCriteria});
} }
const returnData = await getNewEmails(connection, searchCriteria); try {
const returnData = await getNewEmails(connection, searchCriteria);
if (returnData.length) { if (returnData.length) {
this.emit([returnData]); this.emit([returnData]);
}
} catch (error) {
Logger.error('Email Read Imap node encountered an error fetching new emails', { error });
throw error;
} }
} }
}, },