aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
2.96k stars 556 forks source link

DynamoDB Client UpdateItemCommand -- Inaccurate Error Message #6069

Closed lei-zhang-kmart closed 1 month ago

lei-zhang-kmart commented 1 month ago

Checkboxes for prior research

Describe the bug

Attempting to use the UPDATE expression on an existing attribute that is not of type SS will result in an error. This error does not occur persistently. It kept working for a while, I deleted the record, the recreate one, it works again with certain amount of testing.

SDK version number

^2.993.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

node20

Reproduction Steps

public async updateAdjustment(
    locationId: string,
    keycode: string,
    adjustment: number,
    reservedQuantity = 0,
  ): Promise<DocumentClientNS.UpdateItemOutput> {
    let maxAdjustment = this.STARTING_ADJUSTMENT_VALUE
    let maxAdjustmentNegative = this.STARTING_ADJUSTMENT_VALUE
    if (adjustment > this.STARTING_ADJUSTMENT_VALUE) {
      maxAdjustment = -1 * adjustment
    } else if (adjustment < this.STARTING_ADJUSTMENT_VALUE) {
      maxAdjustmentNegative = -1 * adjustment
    }

    const params: DocumentClientNS.UpdateItemInput = {
      TableName: this.config.get('SOH_TABLE_NAME'),
      Key: {
        queryCode: SohOrderAdjustedStockLevelRepository.queryCodeConstructor(locationId, keycode),
      },
      ConditionExpression:
        '(#adjustment <= :maxAdjustment and #adjustment <= :start) or (#adjustment <= :maxAdjustmentNegative and #adjustment > :start) or (attribute_not_exists(#adjustment) and :delta < :start)',
      UpdateExpression:
        'SET #adjustment = if_not_exists(#adjustment, :start) + :delta, #reservedQuantity = if_not_exists(#reservedQuantity, :start) + :reservedQuantity, #refreshId = :uuid',
      ExpressionAttributeNames: {
        '#adjustment': 'adjustment',
        '#reservedQuantity': 'reservedQuantity',
        '#refreshId': 'refreshId',
      },
      ExpressionAttributeValues: {
        ':delta': adjustment,
        ':reservedQuantity': reservedQuantity,
        ':start': this.STARTING_ADJUSTMENT_VALUE,
        ':maxAdjustment': maxAdjustment,
        ':maxAdjustmentNegative': maxAdjustmentNegative,
        ':uuid': uuidV4(),
      },
    }

    return this.client.update(params)
  }

Observed Behavior

It is similar to https://github.com/aws/aws-sdk-js-v3/issues/4939

Expected Behavior

If it work, it should keep working with the similar input, if it does not work it should fail consistently...

Possible Solution

No response

Additional Information/Context

It is similar to the issue as in here. https://github.com/aws/aws-sdk-js-v3/issues/4939

aBurmeseDev commented 1 month ago

Hi @lei-zhang-kmart - thanks for reaching out.

I'm not able to reproduce this on my end. Can you share your SDK code that would give us more insight? Also it sounds like this happens intermittently. If I have to guess, there's some mismatch in data type you're updating Or network issue?

Intermittent issue like this takes a lot of factors to take into consideration to find the root cause, so please provide more detailed info if you can.

lei-zhang-kmart commented 1 month ago

Hey @aBurmeseDev ,

Thank you for looking into this. I finally found what the problem is. Our tester write automation scripted to set the values in the DynamoDB record. Instead of setting to numeric 0, it set to the string "0", but from AWS UI, it looks exactly the same...

So our system then perform numeric operation against the string "0", then it kept failing.

Now, I understand the error message now. But I really hope the error message can be a bit clearer. ex. Numeric operation cannot be operated on string "0" in our case.

I will close this this ticket for now. Hopefully, what I said above make sense, and some improvement on the error message can be done on your end via patch update.

github-actions[bot] commented 1 month ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.