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

What's the proper way to check if GetItem didn't found record? #521

Closed
arvenil opened this issue Jan 20, 2016 · 4 comments
Closed

What's the proper way to check if GetItem didn't found record? #521

arvenil opened this issue Jan 20, 2016 · 4 comments
Labels
guidance Question that needs advice or information.

Comments

@arvenil
Copy link

arvenil commented Jan 20, 2016

What's the proper way to check if GetItem found item? Checking for resp.Item length?

    if len(resp.Item) == 0 {
        return nil, fmt.Errorf("Item not found")
    }

Full example

    params := &dynamodb.GetItemInput{
        Key: map[string]*dynamodb.AttributeValue{
            "token": {
                S: aws.String(token),
            },
        },
        ProjectionExpression: aws.String("json"),
        TableName:            aws.String("tokens"),
    }

    resp, err := self.db.GetItem(params)
    if err != nil {
        return nil, err
    }

    if len(resp.Item) == 0 {
        return nil, fmt.Errorf("Item not found")
    }
@jasdel jasdel added the guidance Question that needs advice or information. label Jan 20, 2016
@jasdel
Copy link
Contributor

jasdel commented Jan 20, 2016

Correct, checking the length of resp.Item is the best way to know if a GetItem request returned a result. Are you seeing in issues where an empty map for Item causes conflicts?

@jasdel jasdel added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jan 20, 2016
@arvenil
Copy link
Author

arvenil commented Jan 20, 2016

The only issue I had was that I first didn't had the check if len(resp.Item) == 0 and so my code was panicking when I tried to get data with resp.Item["json"].
I think this a bit unusual approach, I would rather expect GetItem() returning a package defined error like ErrNotFound (example from sql world https://golang.org/pkg/database/sql/#pkg-variables). Harder to make mistake and end up with panic.

Thanks for answer!

@arvenil arvenil changed the title [question] What's the proper way to check if GetItem didn't found record? What's the proper way to check if GetItem didn't found record? Jan 20, 2016
@jasdel
Copy link
Contributor

jasdel commented Jan 21, 2016

Thanks for the feedback @arvenil I'll make sure to forward that along to the DynamoDB team. Glad to help. Let us know if you have any other questions, feedback, or ideas/features to improve the SDK.

@jasdel jasdel closed this as completed Jan 21, 2016
@0xdevalias
Copy link

Just ran into this.. strangest behaviour, would not have expected it. At the very least some extra documentation somewhere on the function would be useful, it seems ambiguous at best:

If there is no matching item, GetItem does not return any data
// and there will be no Item element in the response.

ErrCodeResourceNotFoundException "ResourceNotFoundException"
The operation tried to access a nonexistent table or index. The resource
might not be specified correctly, or its status might not be ACTIVE.

Thanks for this post existing though.

@diehlaws diehlaws removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Jan 30, 2019
skotambkar pushed a commit to skotambkar/aws-sdk-go that referenced this issue May 20, 2021
…ws#537)

Fixes the SDK's adding the request metadata headers in the wrong
location within the request handler stack. This created a situation
where a request that was retried would sign the new attempt using the
old value of the header. The header value would then be changed before
sending the request.

This moves the request invocation id to only occur during build,
maintaining its value across all requests attempts. Also moves request
retry metadata header to be before sign, and after build. This ensures
that a new value for each attempt can be set, and included in each
request attempt.

Fix aws#533
Fix aws#521
skotambkar pushed a commit to skotambkar/aws-sdk-go that referenced this issue May 20, 2021
Breaking Change
---
* `aws/endpoints`: Several functions and types have been removed
  * Removes `DecodeModel` and `DecodeModelOptions` from the package ([aws#509](aws/aws-sdk-go-v2#509))
  * Remove Region Constants, Partition Constants, and types use for exploring the endpoint data model ([aws#512](aws/aws-sdk-go-v2#512))
* `service/s3/s3crypto`: Package and associated encryption/decryption clients have been removed from the SDK ([aws#511](aws/aws-sdk-go-v2#511))
* `aws/external`: Removes several export constants and types ([aws#508](aws/aws-sdk-go-v2#508))
  * No longer exports AWS environment constants used by the external environment configuration loader
  * `DefaultSharedConfigProfile` is now defined an exported constant
* `aws`: `ErrMissingRegion`, `ErrMissingEndpoint`, `ErrStaticCredentialsEmpty` are now concrete error types ([aws#510](aws/aws-sdk-go-v2#510))

Services
---
* Synced the V2 SDK with latest AWS service API definitions.

SDK Features
---
* `aws/signer/v4`: New methods `SignHTTP` and `PresignHTTP` have been added ([aws#519](aws/aws-sdk-go-v2#519))
  * `SignHTTP` replaces `Sign`, and usage of `Sign` should be migrated before it's removal at a later date
  * `PresignHTTP` replaces `Presign`, and usage of `Presign` should be migrated before it's removal at a later date
  * `DisableRequestBodyOverwrite` and `UnsignedPayload` are now deprecated options and have no effect on `SignHTTP` or `PresignHTTP`. These options will be removed at a later date.
* `aws/external`: Add Support for setting a default fallback region and resolving region from EC2 IMDS ([aws#523](aws/aws-sdk-go-v2#523))
  * `WithDefaultRegion` helper has been added which can be passed to `LoadDefaultAWSConfig`
    * This helper can be used to configure a default fallback region in the event a region fails to be resolved from other sources
  * Support has been added to resolve region using EC2 IMDS when available
    * The IMDS region will be used if region as not found configured in either the shared config or the process environment.
  * Fixes [aws#244](aws/aws-sdk-go-v2#244)
  * Fixes [aws#515](aws/aws-sdk-go-v2#515)

SDK Enhancements
---
* `service/dynamodb/expression`: Add IsSet helper for ConditionBuilder and KeyConditionBuilder ([aws#494](aws/aws-sdk-go-v2#494))
  * Adds a IsSet helper for ConditionBuilder and KeyConditionBuilder to make it easier to determine if the condition builders have any conditions added to them.
  * Implements [aws#493](aws/aws-sdk-go-v2#493).
* `internal/ini`: Normalize Section keys to lowercase ([aws#495](aws/aws-sdk-go-v2#495))
  * Update's SDK's ini utility to store all keys as lowercase. This brings the SDK inline with the AWS CLI's behavior.


SDK Bugs
---
* `internal/sdk`: Fix SDK's UUID utility to handle partial read ([aws#536](aws/aws-sdk-go-v2#536))
  * Fixes the SDK's UUID utility to correctly handle partial reads from its crypto rand source. This error was sometimes causing the SDK's InvocationID value to fail to be obtained, due to a partial read from crypto.Rand.
  * Fix [aws#534](aws/aws-sdk-go-v2#534)
* `aws/defaults`: Fix request metadata headers causing signature errors ([aws#536](aws/aws-sdk-go-v2#536))
    * Fixes the SDK's adding the request metadata headers in the wrong location within the request handler stack. This created a situation where a request that was retried would sign the new attempt using the old value of the header. The header value would then be changed before sending the request.
    * Fix [aws#533](aws/aws-sdk-go-v2#533)
    * Fix [aws#521](aws/aws-sdk-go-v2#521)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

4 participants