Skip to content

Instantly share code, notes, and snippets.

@haitch
Created April 10, 2022 07:07
Show Gist options
  • Save haitch/c133b92ae1d42ae02890dad7ad45b3af to your computer and use it in GitHub Desktop.
Save haitch/c133b92ae1d42ae02890dad7ad45b3af to your computer and use it in GitHub Desktop.
azcosmos ContainerClient.ReadItem() api design flaws

Canot distinguash on 404 vs other errors.

1. ContainerClient.ReadItem() returns internal error (github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos)

error type is github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared.ResponseError, which cannot be used with errors.As

imports github.com/Azure/azure-sdk-for-go/sdk/azcore/internal/shared: build constraints exclude all Go files in /Users/haitch/go/pkg/mod/github.com/!azure/azure-sdk-for-go/sdk/azcore@v0.23.0/internal/shared

2. the error String is huge and not helpful

GET https://xxxx.documents.azure.com:443/dbs/xxxx/colls/xxxx/docs/bm90LWV4aXN0cy1kb2N1bWVudC1pZA
--------------------------------------------------------------------------------
RESPONSE 404: 404 Not Found
ERROR CODE: NotFound
--------------------------------------------------------------------------------
{
  "code": "NotFound",
  "message": "Entity with the specified id does not exist in the system. More info: https://aka.ms/cosmosdb-tsg-not-found, \r\nRequestStartTime: 2022-04-10T06:50:29.4549940Z, RequestEndTime: 2022-04-10T06:50:29.4549940Z,  Number of regions attempted:1\r\n{\"systemHistory\":[{\"dateUtc\":\"2022-04-10T06:49:28.7245594Z\",\"cpu\":2.231,\"memory\":428563352.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0166,\"availableThreads\":32762,\"minThreads\":40,\"maxThreads\":32767}},{\"dateUtc\":\"2022-04-10T06:49:38.7346652Z\",\"cpu\":3.468,\"memory\":429484620.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0238,\"availableThreads\":32765,\"minThreads\":40,\"maxThreads\":32767}},{\"dateUtc\":\"2022-04-10T06:49:48.7447365Z\",\"cpu\":1.916,\"memory\":429666960.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0103,\"availableThreads\":32762,\"minThreads\":40,\"maxThreads\":32767}},{\"dateUtc\":\"2022-04-10T06:49:58.7547995Z\",\"cpu\":0.658,\"memory\":429515804.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0213,\"availableThreads\":32765,\"minThreads\":40,\"maxThreads\":32767}},{\"dateUtc\":\"2022-04-10T06:50:08.7649017Z\",\"cpu\":0.691,\"memory\":429184104.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0324,\"availableThreads\":32764,\"minThreads\":40,\"maxThreads\":32767}},{\"dateUtc\":\"2022-04-10T06:50:18.7748870Z\",\"cpu\":0.687,\"memory\":428968572.000,\"threadInfo\":{\"isThreadStarving\":\"False\",\"threadWaitIntervalInMs\":0.0249,\"availableThreads\":32765,\"minThreads\":40,\"maxThreads\":32767}}]}\r\nRequestStart: 2022-04-10T06:50:29.4549940Z; ResponseTime: 2022-04-10T06:50:29.4549940Z; StoreResult: StorePhysicalAddress: rntbd://cdb-ms-prod-northcentralus1-fd14.documents.azure.com:14096/apps/91a5d8eb-7c7e-4b40-bf9c-269041b81a21/services/cfa9e6b9-c360-47f9-8c0e-3ef86a4a1555/partitions/ff757c70-8c3d-4b5d-93a3-025134a4a56f/replicas/132937695024563992s/, LSN: 6, GlobalCommittedLsn: 6, PartitionKeyRangeId: 0, IsValid: True, StatusCode: 404, SubStatusCode: 0, RequestCharge: 1, ItemLSN: -1, SessionToken: -1#6, UsingLocalLSN: True, TransportException: null, BELatencyMs: 0.273, ActivityId: ec0f89cf-8f14-4f1c-99d0-37d3c0fed187, RetryAfterInMs: , TransportRequestTimeline: {\"requestTimeline\":[{\"event\": \"Created\", \"startTimeUtc\": \"2022-04-10T06:50:29.4549940Z\", \"durationInMs\": 0.0134},{\"event\": \"ChannelAcquisitionStarted\", \"startTimeUtc\": \"2022-04-10T06:50:29.4550074Z\", \"durationInMs\": 0.0112},{\"event\": \"Pipelined\", \"startTimeUtc\": \"2022-04-10T06:50:29.4550186Z\", \"durationInMs\": 0.1679},{\"event\": \"Transit Time\", \"startTimeUtc\": \"2022-04-10T06:50:29.4551865Z\", \"durationInMs\": 1.2691},{\"event\": \"Received\", \"startTimeUtc\": \"2022-04-10T06:50:29.4564556Z\", \"durationInMs\": 0.0856},{\"event\": \"Completed\", \"startTimeUtc\": \"2022-04-10T06:50:29.4565412Z\", \"durationInMs\": 0}],\"requestSizeInBytes\":575,\"responseMetadataSizeInBytes\":140,\"responseBodySizeInBytes\":87};\r\n ResourceType: Document, OperationType: Read\r\n, Microsoft.Azure.Documents.Common/2.14.0"
}
--------------------------------------------------------------------------------

3. the itemResponse is nil

Which I cannot use to read the response code.

package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
)
func main() {
cosmosClient, err := manualDBClient("https://xxxxxxxx.documents.azure.com:443/", "xxxxxxxx")
if err != nil {
panic("invalid documentDB credential")
}
containerClient, err := cosmosClient.NewContainer("xxxxxxxx", "xxxxxxxx")
if err != nil {
panic("invalid documentDB database/container")
}
pk := azcosmos.NewPartitionKeyString("not-exists-partition-key")
itemId := base64.RawURLEncoding.EncodeToString([]byte("not-exists-document-id"))
resp, err := containerClient.ReadItem(context.Background(), pk, itemId, nil)
if err != nil {
errStr := err.Error()
fmt.Println("=========Error String=========")
fmt.Println(errStr)
fmt.Println("=========Item Response=========")
fmt.Println(resp)
}
}
func manualDBClient(endpoint, key string) (*azcosmos.Client, error) {
keyCred, err := azcosmos.NewKeyCredential(key)
if err != nil {
return nil, err
}
cosmosClient, err := azcosmos.NewClientWithKey(endpoint, keyCred, nil)
if err != nil {
return nil, err
}
return cosmosClient, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment