Skip to content
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

Unhandled Promise Rejection on ALB event #678

Open
repl-ullas-ml opened this issue Apr 23, 2024 · 2 comments
Open

Unhandled Promise Rejection on ALB event #678

repl-ullas-ml opened this issue Apr 23, 2024 · 2 comments

Comments

@repl-ullas-ml
Copy link

repl-ullas-ml commented Apr 23, 2024

Given an ALB event with multiValueQueryStringParameters but one of the value fails decodeURIComponent , the promise isnt handled at all. Expectation is that it should return an error and consumers treat that error

path: getPathWithQueryStringUseUnescapeParams({ event })

query[formattedKey] = event.multiValueQueryStringParameters[key].map(value => decodeUrlencoded(value))

The below test wont even report the failure since the promise is left unhandled

  test('serverlessExpressInstance should throw', async () => {
    const multiValueQueryStringParameters = { etype: ['odp'], passurl: ['/category/'], template: ['../../../../../../../../../etc/passwd%%0000.html'] }

    const event = makeEvent({
      eventSourceName: 'alb',
      path: '/',
      httpMethod: 'GET',
      multiValueQueryStringParameters
    })
    await expect(serverlessExpressInstance(event)).toThrow('some')
  })

Screenshot 2024-04-23 at 11 34 20 AM

@brettstack
Copy link
Collaborator

Thanks for the report. Serverless Express catches errors and returns to a response to the event source with the error in a format expected by the event source. For example, API Gateway expects a statusCode so it returns 500 along with the error. This is usually what you want, since if the Lambda throws an error, API Gateway returns a different error to the client. See:

https://github.com/CodeGenieApp/serverless-express/blob/mainline/src/configure.js#L72-L95 which catches the error, and

https://github.com/CodeGenieApp/serverless-express/blob/mainline/src/transport.js#L62-L80 which determines how to respond. Note line 63 ALB is included in the list of services that will include a response format rather than throwing the error.

If you really want to throw an error, you can do something like const response = await serverlessExpressInstance(event) and inspect it for a 500, and then throw.

@repl-ullas-ml
Copy link
Author

repl-ullas-ml commented Oct 15, 2024

@brettstack I carefully looked into this once again and the call stack never shows a hit on https://github.com/CodeGenieApp/serverless-express/blob/mainline/src/configure.js#L86 .. so https://github.com/CodeGenieApp/serverless-express/blob/mainline/src/transport.js#L62-L80 which determines how to respond is never executed.

I still get Unhandled Promise Rejection on this and never hitting either of response received nor Unhandled error

try {
    const response = await serverlessExpressInstance(event);
    logger.error( 'response received')
    return response;
  }
catch (e) {
 logger.error({ err: err }, 'Unhandled error')
}

Please note: we have multi value headers enabled. Not sure if thats something of interest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants