azure-sdk-for-js icon indicating copy to clipboard operation
azure-sdk-for-js copied to clipboard

@azure/cosmos - upsert doesn't quickly fail

Open diberry opened this issue 3 years ago • 1 comments

  • Package Name: @azure/cosmos
  • Package Version: 3.17.1
  • Operating system: Win11
  • [ ] nodejs
    • version: 14
  • [ ] browser
    • name/version:
  • [ ] typescript
    • version:
  • Is the bug related to documentation in
    • [ ] README.md
    • [ ] source code documentation
    • [ ] SDK API docs on https://docs.microsoft.com

Describe the bug If I run a script which attempts to upsert a doc and the connection string is correct but the string names for db, container, or partition key are incorrect, the upsert doesn't quickly timeout or fail like other SDKs do.

To Reproduce Steps to reproduce the behavior:

  1. Script at bottom of issue.
  2. Create .env file with correct connection string but incorrect db, container, and partition key
  3. Run script.
  4. Script hangs (doesn't fail) at .upsert instead of quickly failing

Expected behavior Expect quick failure if objects in operation chain don't exist.

JavaScript ES6 (type: module) Code

import * as path from "path";
import { promises as fs } from "fs";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Get environment variables from .env
import * as dotenv from "dotenv";
dotenv.config();

// Get Cosmos Client
import { CosmosClient } from "@azure/cosmos";

// Provide required connection from environment variables
const cosmosSecret = process.env.COSMOS_CONNECTION_STRING;

// Authenticate to Azure Cosmos DB
const cosmosClient = new CosmosClient(cosmosSecret);

// Set Database name and container name
const databaseName = process.env.COSMOS_DATABASE_NAME;
const containerName = process.env.COSMOS_CONTAINER_NAME;

// Get Container
const container = await cosmosClient.database(databaseName).container(containerName);

// Either insert or update item
async function upsert(fileAndPathToJson, encoding='utf-8') {

  // Get item from file
  const data = JSON.parse(await fs.readFile(path.join(__dirname, fileAndPathToJson), encoding));

  // Process request
  // result.resource is the returned item
  const result = await container.items.upsert(data);

  if(result.statusCode===201){
    console.log("Inserted data");
  } else if (result.statusCode===200){
    console.log("Updated data");
  } else {
    console.log(`unexpected statusCode ${result.statusCode}`);
  }
}

// Insert data - statusCode = 201
await upsert('./3-contoso-products-upsert-insert.json');

// Update data - statusCode = 200
await upsert('./3-contoso-products-upsert-update.json');

// Get item from container and partition key
const { resource } = await container.item("123", "xyz").read();

// Show final item
console.log(resource);

.env file contents

COSMOS_CONNECTION_STRING=CORRECT-CONNECTION-STRING
COSMOS_DATABASE_NAME=INCORRECT-DB-NAME
COSMOS_CONTAINER_NAME=INCORRECT-CONTAINER-NAME
COSMOS_CONTAINER_PARTITION_KEY=PARTITION-KEY-DOESNT-EXIST-IN-DOCS

diberry avatar Sep 29 '22 17:09 diberry

@diberry I tried to reproduce this issue on my system. I used 3.17.1 version and tried to give non-existing names for database and container. I got resource doesn't exist in response without much delay. Maybe I am missing something can you please share more details here?

  body: {
    code: 'NotFound',
    message: 'Message: {"Errors":["Resource Not Found. Learn more: https:\\/\\/aka.ms\\/cosmosdb-tsg-not-found"]}

topshot99 avatar Oct 06 '22 10:10 topshot99

Since there is no response on this , we are closing this issue. Please feel free to re open the issue with details.

sajeetharan avatar Jan 10 '23 06:01 sajeetharan