Skip to content

Commit b41028c

Browse files
committed
Merge remote-tracking branch 'origin/fix/sendemail-error-catch'
2 parents 0f9c6b6 + ea47736 commit b41028c

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

src/email/build.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ const ensureConfigHasFrom = (emailConfig) => {
2626
}
2727
};
2828

29+
const handleMockAccount = async (emailConfig: EmailOptions) => {
30+
let mockAccount;
31+
try {
32+
mockAccount = await mockHandler(emailConfig);
33+
const { account: { web, user, pass } } = mockAccount;
34+
logger.info('E-mail configured with mock configuration');
35+
logger.info(`Log into mock email provider at ${web}`);
36+
logger.info(`Mock email account username: ${user}`);
37+
logger.info(`Mock email account password: ${pass}`);
38+
} catch (err) {
39+
logger.error(
40+
'There was a problem setting up the mock email handler',
41+
err,
42+
);
43+
}
44+
return mockAccount;
45+
};
46+
2947
export default async function buildEmail(emailConfig: EmailOptions): BuildEmailResult {
3048
if (hasTransport(emailConfig) && emailConfig.transport) {
3149
ensureConfigHasFrom(emailConfig);
@@ -41,12 +59,5 @@ export default async function buildEmail(emailConfig: EmailOptions): BuildEmailR
4159
return handleTransport(transport, email);
4260
}
4361

44-
const mockAccount = await mockHandler(emailConfig);
45-
// Only log mock credentials if was explicitly set in config
46-
const { account: { web, user, pass } } = mockAccount;
47-
logger.info('E-mail configured with mock configuration');
48-
logger.info(`Log into mock email provider at ${web}`);
49-
logger.info(`Mock email account username: ${user}`);
50-
logger.info(`Mock email account password: ${pass}`);
51-
return mockAccount;
62+
return handleMockAccount(emailConfig);
5263
}

src/email/sendEmail.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Message } from './types';
2+
import logger from '../utilities/logger';
3+
4+
export default async function sendEmail(message: Message): Promise<unknown> {
5+
let result;
6+
try {
7+
const email = await this.email;
8+
result = email.transport.sendMail(message);
9+
} catch (err) {
10+
logger.error(
11+
`Failed to send mail to ${message.to}, subject: ${message.subject}`,
12+
err,
13+
);
14+
return err;
15+
}
16+
return result;
17+
}

src/index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import localGlobalOperations from './globals/operations/local';
3434
import { encrypt, decrypt } from './auth/crypto';
3535
import { MockEmailHandler, BuildEmailResult, Message } from './email/types';
3636
import { PayloadRequest } from './express/types';
37+
import sendEmail from './email/sendEmail';
3738

3839
import { Options as CreateOptions } from './collections/operations/local/create';
3940
import { Options as FindOptions } from './collections/operations/local/find';
@@ -67,6 +68,8 @@ export class Payload {
6768

6869
email: BuildEmailResult;
6970

71+
sendEmail: (message: Message) => Promise<unknown>;
72+
7073
license: string;
7174

7275
secret: string;
@@ -133,6 +136,7 @@ export class Payload {
133136

134137
// Configure email service
135138
this.email = buildEmail(this.emailOptions);
139+
this.sendEmail = sendEmail.bind(this);
136140

137141
// Initialize collections & globals
138142
initCollections(this);
@@ -184,17 +188,6 @@ export class Payload {
184188
if (typeof options.onInit === 'function') options.onInit(this);
185189
}
186190

187-
sendEmail = async (message: Message): Promise<unknown> => {
188-
const email = await this.email;
189-
const result = email.transport.sendMail(message);
190-
return result;
191-
}
192-
193-
getMockEmailCredentials = async (): Promise<TestAccount> => {
194-
const email = await this.email as MockEmailHandler;
195-
return email.account;
196-
}
197-
198191
getAdminURL = (): string => `${this.config.serverURL}${this.config.routes.admin}`;
199192

200193
getAPIURL = (): string => `${this.config.serverURL}${this.config.routes.api}`;

0 commit comments

Comments
 (0)