-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(email-plugin): Improve error logging
Closes #574
- Loading branch information
1 parent
c4bed2d
commit 70cb932
Showing
5 changed files
with
114 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,15 @@ import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants'; | |
import { | ||
EventBus, | ||
LanguageCode, | ||
Logger, | ||
Order, | ||
OrderStateTransitionEvent, | ||
PluginCommonModule, | ||
ProcessContextModule, | ||
RequestContext, | ||
VendureEvent, | ||
} from '@vendure/core'; | ||
import { TestingLogger } from '@vendure/testing'; | ||
import path from 'path'; | ||
|
||
import { orderConfirmationHandler } from './default-email-handlers'; | ||
|
@@ -25,6 +27,8 @@ describe('EmailPlugin', () => { | |
let onSend: jest.Mock; | ||
let module: TestingModule; | ||
|
||
const testingLogger = new TestingLogger(() => jest.fn()); | ||
|
||
async function initPluginWithHandlers( | ||
handlers: Array<EmailEventHandler<string, any>>, | ||
options?: Partial<EmailPluginOptions>, | ||
|
@@ -51,6 +55,9 @@ describe('EmailPlugin', () => { | |
providers: [MockService], | ||
}).compile(); | ||
|
||
Logger.useLogger(testingLogger); | ||
module.useLogger(new Logger()); | ||
|
||
const plugin = module.get(EmailPlugin); | ||
eventBus = module.get(EventBus); | ||
await plugin.onVendureBootstrap(); | ||
|
@@ -492,6 +499,71 @@ describe('EmailPlugin', () => { | |
expect(onSend.mock.calls[0][0].subject).toBe(`Order confirmation for #${order.code}`); | ||
}); | ||
}); | ||
|
||
describe('error handling', () => { | ||
it('Logs an error if the template file is not found', async () => { | ||
const ctx = RequestContext.deserialize({ | ||
_channel: { code: DEFAULT_CHANNEL_CODE }, | ||
_languageCode: LanguageCode.en, | ||
} as any); | ||
testingLogger.errorSpy.mockClear(); | ||
|
||
const handler = new EmailEventListener('no-template') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}'); | ||
|
||
await initPluginWithHandlers([handler]); | ||
|
||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
expect(testingLogger.errorSpy.mock.calls[0][0]).toContain(`ENOENT: no such file or directory`); | ||
}); | ||
|
||
it('Logs a Handlebars error if the template is invalid', async () => { | ||
const ctx = RequestContext.deserialize({ | ||
_channel: { code: DEFAULT_CHANNEL_CODE }, | ||
_languageCode: LanguageCode.en, | ||
} as any); | ||
testingLogger.errorSpy.mockClear(); | ||
|
||
const handler = new EmailEventListener('bad-template') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}'); | ||
|
||
await initPluginWithHandlers([handler]); | ||
|
||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
expect(testingLogger.errorSpy.mock.calls[0][0]).toContain(`Parse error on line 3:`); | ||
}); | ||
|
||
it('Logs an error if the loadData method throws', async () => { | ||
const ctx = RequestContext.deserialize({ | ||
_channel: { code: DEFAULT_CHANNEL_CODE }, | ||
_languageCode: LanguageCode.en, | ||
} as any); | ||
testingLogger.errorSpy.mockClear(); | ||
|
||
const handler = new EmailEventListener('bad-template') | ||
.on(MockEvent) | ||
.setFrom('"test from" <[email protected]>') | ||
.setRecipient(() => '[email protected]') | ||
.setSubject('Hello {{ subjectVar }}') | ||
.loadData(context => { | ||
throw new Error('something went horribly wrong!'); | ||
}); | ||
|
||
await initPluginWithHandlers([handler]); | ||
|
||
eventBus.publish(new MockEvent(ctx, true)); | ||
await pause(); | ||
expect(testingLogger.errorSpy.mock.calls[0][0]).toContain(`something went horribly wrong!`); | ||
}); | ||
}); | ||
}); | ||
|
||
const pause = () => new Promise(resolve => setTimeout(resolve, 100)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{ this is a bad Handlbars template }} | ||
|
||
{{ < designed }} | ||
|
||
{{ to produce an error!! }}} |