Skip to content

[search-documents] getDocument throws TypeError when error response body is undefined #38842

@adamalfredsson

Description

@adamalfredsson
  • Package Name: @azure/search-documents
  • Package Version: 13.0.0
  • Operating system: macOS (darwin 25.5.0)
  • nodejs
    • version: 22.14.0
  • browser
    • name/version:
  • typescript
    • version: 5.9.3
  • Is the bug related to documentation in

Describe the bug

When SearchClient.getDocument() is called for a document key that does not exist, Azure Search returns a non-200 response (404) with an empty/undefined response body. The SDK then crashes while deserializing the error response instead of throwing a proper RestError.

errorResponseDeserializer in dist/esm/models/azure/search/documents/models.js accesses item["error"] without checking that item is defined. _getDocumentDeserialize passes result.body directly:

error.details = errorResponseDeserializer(result.body);

When result.body is undefined, this throws:

TypeError: Cannot read properties of undefined (reading 'error')

Callers expecting a RestError with statusCode: 404 cannot handle the failure as a normal not-found case.

To Reproduce

Steps to reproduce the behavior:

  1. Create a SearchClient for an existing index (API version defaults to 2026-04-01).
  2. Call getDocument() with a key that does not exist in the index.
  3. Observe that the SDK throws TypeError: Cannot read properties of undefined (reading 'error') instead of a RestError with status code 404.

Minimal example:

import { AzureKeyCredential, SearchClient } from "@azure/search-documents";

const client = new SearchClient(
  process.env.AZURE_SEARCH_ENDPOINT!,
  "my-index",
  new AzureKeyCredential(process.env.AZURE_SEARCH_KEY!)
);

// Use a UUID/document key that does not exist in the index
await client.getDocument("4e7ea399-7f14-4ce5-8fa1-bbc7020cc25f");

Expected behavior

The SDK should throw a RestError (or equivalent Azure client error) with statusCode: 404 when the document is not found, even if the error response body is empty or missing. errorResponseDeserializer should guard against undefined input.

Screenshots

N/A

Additional context

  • Stack trace points to:
    • errorResponseDeserializerdist/esm/models/azure/search/documents/models.js:7
    • _getDocumentDeserializedist/esm/search/api/operations.js:261
    • getDocumentdist/esm/search/api/operations.js:269
  • The same pattern (errorResponseDeserializer(result.body)) appears in many other operation deserializers, so they may be affected by empty error bodies too.
  • Workaround: guard in errorResponseDeserializer:
export function errorResponseDeserializer(item) {
    if (!item) {
        return { error: undefined };
    }
    return {
        error: !item["error"] ? item["error"] : errorDetailDeserializer(item["error"]),
    };
}
  • Reproduced in a Next.js server environment using ESM (import), but the issue is in the SDK's compiled output, not app-specific bundling.

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Searchcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type
No fields configured for issues without a type.

Projects

Status
Untriaged

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions