-
Notifications
You must be signed in to change notification settings - Fork 200
fix(batch): throw when a synchronous processor gets a promise-returning handler #5650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vahidshaik1901
wants to merge
3
commits into
aws-powertools:main
Choose a base branch
from
vahidshaik1901:fix/sync-batch-processor-promise-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
| import context from '@aws-lambda-powertools/testing-utils/context'; | ||
| import type { SQSRecord } from 'aws-lambda'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| AsyncHandlerNotSupportedError, | ||
| BatchProcessorSync, | ||
| EventType, | ||
| processPartialResponseSync, | ||
| SqsFifoPartialProcessor, | ||
| } from '../../src/index.js'; | ||
| import { sqsRecordFactory } from '../helpers/factories.js'; | ||
|
|
||
| describe('Class: BatchProcessorSync', () => { | ||
| const asyncRecordHandler = async (record: SQSRecord): Promise<string> => | ||
| record.body; | ||
|
|
||
| const rejectingRecordHandler = async ( | ||
| _record: SQSRecord | ||
| ): Promise<string> => { | ||
| throw new Error('failed'); | ||
| }; | ||
|
|
||
| it('throws when the record handler returns a promise', () => { | ||
| // Prepare | ||
| const records = [sqsRecordFactory('success'), sqsRecordFactory('success')]; | ||
| const batch = { Records: records }; | ||
| const processor = new BatchProcessorSync(EventType.SQS); | ||
|
|
||
| // Act & Assess | ||
| expect(() => | ||
| processPartialResponseSync(batch, asyncRecordHandler, processor, { | ||
| context, | ||
| }) | ||
| ).toThrow(AsyncHandlerNotSupportedError); | ||
| expect(processor.successMessages).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('leaves no unhandled rejection behind when the promise rejects', () => { | ||
| // Prepare | ||
| const records = [sqsRecordFactory('success'), sqsRecordFactory('success')]; | ||
| const batch = { Records: records }; | ||
| const processor = new BatchProcessorSync(EventType.SQS); | ||
|
|
||
| // Act & Assess | ||
| // The abandoned promise rejects after the throw, and vitest fails this file | ||
| // if nothing has taken ownership of that rejection. | ||
| expect(() => | ||
| processPartialResponseSync(batch, rejectingRecordHandler, processor, { | ||
| context, | ||
| }) | ||
| ).toThrow(AsyncHandlerNotSupportedError); | ||
| }); | ||
|
|
||
| it('throws when a SqsFifoPartialProcessor record handler returns a promise', () => { | ||
| // Prepare | ||
| const records = [sqsRecordFactory('success'), sqsRecordFactory('success')]; | ||
| const batch = { Records: records }; | ||
| const processor = new SqsFifoPartialProcessor(); | ||
|
|
||
| // Act & Assess | ||
| expect(() => | ||
| processPartialResponseSync(batch, asyncRecordHandler, processor, { | ||
| context, | ||
| }) | ||
| ).toThrow(AsyncHandlerNotSupportedError); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The guard itself makes sense, and I agree the rejection has to be claimed before the throw or the Lambda runtime reports
Runtime.UnhandledPromiseRejectioninstead of this error. Two things about how it's claimed though.The no-op handler discards the customer's actual failure. By this point the
asynchandler has already been invoked for the first record, so if it rejects, that rejection is the only evidence of what went wrong inside their code. With this line they see "wrong processor" and nothing else. I'd rather log the reason so the guard doesn't hide a second bug:Separately, the comment says the code "takes ownership" of the rejection, which reads as if it's handled. It's suppressed, and the comment is clearer if it says that. The suggestion above rewords it.
One more nit, not on this line: since the handler has already run once when this throws, it'd help if
AsyncHandlerNotSupportedErrorsaid so. Something like "The record handler was invoked for the first record and returned a promise, but this batch processor is synchronous and cannot await it..." tells the reader that side effects may already be in flight.