Skip to content

Commit d62f6e8

Browse files
authored
Merge pull request #423 from jgilbert01/issue-fault-not-caught
Handle faults in update flavor toUpdateRequest when provided update request function is not async
2 parents 5e96757 + 68cb66f commit d62f6e8

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-lambda-stream",
3-
"version": "1.1.10",
3+
"version": "1.1.11",
44
"description": "Create stream processors with AWS Lambda functions.",
55
"keywords": [
66
"aws",

src/flavors/update.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
printStartPipeline, printEndPipeline,
33
faulty, faultyAsyncStream, splitObject,
44
compact,
5+
faultify,
56
} from '../utils';
67

78
import {
@@ -63,8 +64,7 @@ const toGetRequest = (rule) => faulty((uow) => ({
6364
: undefined,
6465
}));
6566

66-
const toUpdateRequest = (rule) => faultyAsyncStream((uow) => Promise.resolve(rule.toUpdateRequest(uow, rule))
67-
.then((updateRequest) => ({
68-
...uow,
69-
updateRequest,
70-
})));
67+
const toUpdateRequest = (rule) => faultyAsyncStream(async (uow) => ({
68+
...uow,
69+
updateRequest: await faultify(rule.toUpdateRequest)(uow, rule),
70+
}));

test/unit/flavors/update.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,80 @@ describe('flavors/update.js', () => {
213213
})
214214
.done(done);
215215
});
216+
217+
it('should fault on error', (done) => {
218+
sinon.stub(DynamoDBConnector.prototype, 'query').resolves([]);
219+
sinon.stub(DynamoDBConnector.prototype, 'batchGet').resolves({
220+
Responses: {
221+
undefined: [{
222+
pk: '2',
223+
sk: 'thing',
224+
discriminator: 'thing',
225+
name: 'thing2',
226+
}],
227+
},
228+
UnprocessedKeys: {},
229+
});
230+
const ebStub = sinon.stub(EventBridgeConnector.prototype, 'putEvents').resolves({ FailedEntryCount: 0 });
231+
232+
sinon.stub(KmsConnector.prototype, 'generateDataKey').resolves(MOCK_GEN_DK_RESPONSE);
233+
234+
const events = toDynamodbRecords([
235+
{
236+
timestamp: 1572832690,
237+
keys: {
238+
pk: '1',
239+
sk: 'thing',
240+
},
241+
newImage: {
242+
pk: '1',
243+
sk: 'thing',
244+
discriminator: 'thing',
245+
name: 'Thing One',
246+
description: 'This is thing one',
247+
otherThing: 'thing|2',
248+
ttl: 1549053422,
249+
timestamp: 1548967022000,
250+
},
251+
},
252+
]);
253+
254+
const errorRule = {
255+
id: 'error-rule',
256+
flavor: update,
257+
eventType: /thing-*/,
258+
filters: [() => true],
259+
toGetRequest,
260+
fks: ['otherThing'],
261+
toUpdateRequest: (uow) => { throw new Error('intentional fault'); },
262+
};
263+
264+
initialize({
265+
...initializeFrom([errorRule]),
266+
}, { ...defaultOptions, AES: false })
267+
.assemble(fromDynamodb(events), true)
268+
.collect()
269+
// .tap((collected) => console.log(JSON.stringify(collected, null, 2)))
270+
.tap((collected) => {
271+
expect(collected.length).to.eq(1);
272+
expect(collected[0].event.err.message).to.eq('intentional fault');
273+
expect(ebStub).to.have.been.calledOnceWith({
274+
Entries: [{
275+
EventBusName: 'undefined',
276+
Source: 'custom',
277+
DetailType: 'fault',
278+
Detail: JSON.stringify(collected[0].event),
279+
}],
280+
}, {
281+
batch: [{
282+
event: collected[0].event,
283+
publishRequestEntry: collected[0].publishRequestEntry,
284+
}],
285+
publishRequest: collected[0].publishRequest,
286+
});
287+
})
288+
.done(done);
289+
});
216290
});
217291

218292
const toUpdateRequest = (uow) => ({

0 commit comments

Comments
 (0)