MCPcopy Create free account
hub / github.com/google/go-cloud / ExampleCollection_ErrorAs

Function ExampleCollection_ErrorAs

docstore/example_test.go:143–170  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

141}
142
143func ExampleCollection_ErrorAs() {
144 // This example is specific to the awsdynamodb implementation.
145 // You will need to blank-import the package for this to work:
146 // import _ "gocloud.dev/docstore/awsdynamodb/v2"
147
148 // The types exposed for As by mongodocstore are documented in
149 // https://godoc.org/gocloud.dev/docstore/mongodocstore#hdr-As
150
151 // This URL will open the collection using default credentials.
152 ctx := context.Background()
153 coll, err := docstore.OpenCollection(ctx, "dynamodb://mytable?partition_key=partkey")
154 if err != nil {
155 log.Fatal(err)
156 }
157 defer coll.Close()
158
159 doc := map[string]any{"_id": "a"}
160 if err := coll.Create(ctx, doc); err != nil {
161 var aerr smithy.APIError
162 if coll.ErrorAs(err, &aerr) {
163 fmt.Println("got", aerr)
164 } else {
165 fmt.Println("could not convert error")
166 }
167 // Alternatively, using errors.As:
168 _ = errors.As(err, &aerr)
169 }
170}
171
172func ExampleQuery_Get() {
173 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.

Callers

nothing calls this directly

Calls 5

OpenCollectionFunction · 0.92
CloseMethod · 0.65
ErrorAsMethod · 0.65
AsMethod · 0.65
CreateMethod · 0.45

Tested by

no test coverage detected