fix: better error handler when sendMail fails
This commit is contained in:
@@ -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
17
src/email/sendEmail.ts
Normal 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;
|
||||
}
|
||||
15
src/index.ts
15
src/index.ts
@@ -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}`;
|
||||
|
||||
Reference in New Issue
Block a user