fix: better error handler when sendMail fails

This commit is contained in:
Dan Ribbens
2021-01-14 15:55:42 -05:00
parent 45708771b0
commit ea47736274
3 changed files with 40 additions and 19 deletions

View File

@@ -26,6 +26,24 @@ const ensureConfigHasFrom = (emailConfig) => {
}
};
const handleMockAccount = async (emailConfig: EmailOptions) => {
let mockAccount;
try {
mockAccount = await mockHandler(emailConfig);
const { account: { web, user, pass } } = mockAccount;
logger.info('E-mail configured with mock configuration');
logger.info(`Log into mock email provider at ${web}`);
logger.info(`Mock email account username: ${user}`);
logger.info(`Mock email account password: ${pass}`);
} catch (err) {
logger.error(
'There was a problem setting up the mock email handler',
err,
);
}
return mockAccount;
};
export default async function buildEmail(emailConfig: EmailOptions): BuildEmailResult {
if (hasTransport(emailConfig) && emailConfig.transport) {
ensureConfigHasFrom(emailConfig);
@@ -41,12 +59,5 @@ export default async function buildEmail(emailConfig: EmailOptions): BuildEmailR
return handleTransport(transport, email);
}
const mockAccount = await mockHandler(emailConfig);
// Only log mock credentials if was explicitly set in config
const { account: { web, user, pass } } = mockAccount;
logger.info('E-mail configured with mock configuration');
logger.info(`Log into mock email provider at ${web}`);
logger.info(`Mock email account username: ${user}`);
logger.info(`Mock email account password: ${pass}`);
return mockAccount;
return handleMockAccount(emailConfig);
}

17
src/email/sendEmail.ts Normal file
View File

@@ -0,0 +1,17 @@
import { Message } from './types';
import logger from '../utilities/logger';
export default async function sendEmail(message: Message): Promise<unknown> {
let result;
try {
const email = await this.email;
result = email.transport.sendMail(message);
} catch (err) {
logger.error(
`Failed to send mail to ${message.to}, subject: ${message.subject}`,
err,
);
return err;
}
return result;
}

View File

@@ -34,6 +34,7 @@ import localGlobalOperations from './globals/operations/local';
import { encrypt, decrypt } from './auth/crypto';
import { MockEmailHandler, BuildEmailResult, Message } from './email/types';
import { PayloadRequest } from './express/types';
import sendEmail from './email/sendEmail';
import { Options as CreateOptions } from './collections/operations/local/create';
import { Options as FindOptions } from './collections/operations/local/find';
@@ -67,6 +68,8 @@ export class Payload {
email: BuildEmailResult;
sendEmail: (message: Message) => Promise<unknown>;
license: string;
secret: string;
@@ -133,6 +136,7 @@ export class Payload {
// Configure email service
this.email = buildEmail(this.emailOptions);
this.sendEmail = sendEmail.bind(this);
// Initialize collections & globals
initCollections(this);
@@ -184,17 +188,6 @@ export class Payload {
if (typeof options.onInit === 'function') options.onInit(this);
}
sendEmail = async (message: Message): Promise<unknown> => {
const email = await this.email;
const result = email.transport.sendMail(message);
return result;
}
getMockEmailCredentials = async (): Promise<TestAccount> => {
const email = await this.email as MockEmailHandler;
return email.account;
}
getAdminURL = (): string => `${this.config.serverURL}${this.config.routes.admin}`;
getAPIURL = (): string => `${this.config.serverURL}${this.config.routes.api}`;