Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,5 @@ aws lambda invoke \
## Debugging

Set `DEBUG=1` (or any non-empty value) to enable additional log output. This can help troubleshoot event dispatching and handler execution.

When invocation data is collected, functions on the `context` object are invoked. If a context function throws an error, its message is captured and included in the logged context instead of halting execution.
11 changes: 10 additions & 1 deletion collectInvocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* The raw `context` object exposes functions, so we copy its enumerable
* properties while invoking any functions to capture their returned values.
* If a function throws, its error message is stored instead of aborting.
* This keeps test snapshots stable and avoids leaking functions.
*
* @param {object} event - Event payload provided to the Lambda.
Expand All @@ -14,7 +15,15 @@ export default function collectInvocation(event, context, handlerType) {
const ctx = {};
if (context) {
for (const [key, value] of Object.entries(context)) {
ctx[key] = typeof value === 'function' ? value.call(context) : value;
if (typeof value === 'function') {
try {
ctx[key] = value.call(context);
} catch (err) {
ctx[key] = err && err.message ? err.message : String(err);
}
} else {
ctx[key] = value;
}
}
}
return { event, context: ctx, handlerType };
Expand Down
16 changes: 16 additions & 0 deletions tests/collectInvocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ describe('collectInvocation', () => {
handlerType: 'test'
});
});

test('handles context functions that throw', () => {
const event = {};
const context = {
ok: () => 'good',
fail() { throw new Error('boom'); }
};

const result = collectInvocation(event, context, 'test');

expect(result).toEqual({
event,
context: { ok: 'good', fail: 'boom' },
handlerType: 'test'
});
});
});
Loading