middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵

Home Page:https://middy.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@middy/dynamodb middleware: Unmarshall on empty item is causing exception

xle opened this issue · comments

commented

Describe the bug
Hi guys

Dont know if its really a bug but when using the dynamodb middleware if the query is returning an empty result an exception is thrown which can make the lambda return an internal error for the first call (second immediate call the value is cached).

when looking at the code i see that the case of item empty or undefined is not check before calling unmarshall.

     client
        .send(new GetItemCommand(inputParameters))
        .then((resp) => unmarshall(resp.Item))
        .catch((e) => {
          const value = getCache(options.cacheKey).value ?? {}
          value[internalKey] = undefined
          modifyCache(options.cacheKey, value)
          throw e
        })

To Reproduce
configure the middleware with a query returning empty value

Expected behaviour
maybe not throwing the error in the catch as we are putting the value in cache or checking empty item.

Environment (please complete the following information):

  • Node.js: 18
  • Middy: 5.0.3
  • AWS SDK 3.54.0

Thanks for reporting. We use @aws-sdk/util-dynamodb internally for unmarshall. I'm surprised it doesn't support this. I would suggest opening an issue at https://github.com/aws/aws-sdk-js-v3. If you don't get a response, we have a faster unmarshall implementation inside of @middy/event-normalizer that we can swap in that does support empty/undefined values.

commented

thanks for your answer. I checked the code in the event-normalizer and that will solved the problem as its the sdk convertToNative function who throw an exception when data is empty.
I will open an issue for them.
In the same time in order to solve my problem quickly and before any implementation decisions is taken, i will implement my own middleware using your unmarshall method.

I'm going to close for now, please reopen if we need to revisit this.
I am curious why you're fetching an item that is undefined and the use case around it. The original idea around this middleware was to pull a config used for all requests, in this case it would always exist.