Added error handling for email analytics unsubscribe event (#16613)

refs TryGhost/Team#2974

- currently the unsubscribeFromNewsletters event is failing with 'member
not found' in elastic
- this change catches the error and logs it, which should allow the rest
of the event(s) to be processed
This commit is contained in:
Chris Raible 2023-04-11 13:13:34 -07:00 committed by GitHub
parent b820853b52
commit 0b0e3f8e85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -129,7 +129,11 @@ class EmailEventStorage {
}
async unsubscribeFromNewsletters(event) {
try {
await this.#membersRepository.update({newsletters: []}, {id: event.memberId});
} catch (err) {
logging.error(err);
}
}
}

View File

@ -466,6 +466,27 @@ describe('Email Event Storage', function () {
assert(update.firstCall.args[0].newsletters.length === 0);
});
it('Handles unsubscribe with a non-existent member', async function () {
const event = EmailUnsubscribedEvent.create({
email: 'example@example.com',
memberId: '123',
emailId: '456',
timestamp: new Date(0)
});
const error = new Error('Member not found');
const update = sinon.stub().throws(error);
const eventHandler = new EmailEventStorage({
membersRepository: {
update
}
});
await eventHandler.handleUnsubscribed(event);
assert(update.calledOnce);
assert(update.firstCall.args[0].newsletters.length === 0);
});
it('Handles complaints', async function () {
const event = SpamComplaintEvent.create({
email: 'example@example.com',