Skip to content

Conversation

codetheweb
Copy link
Contributor

@codetheweb codetheweb commented Oct 19, 2025

Description of changes

Prior to this change, responses containing error messages would never be visible.

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Migration plan

Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?

Observability plan

What is the plan to instrument and monitor this change?

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs section?

Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

Copy link
Contributor Author

codetheweb commented Oct 19, 2025

@codetheweb codetheweb marked this pull request as ready for review October 19, 2025 23:32
@codetheweb codetheweb requested a review from rescrv October 19, 2025 23:32
Copy link
Contributor

propel-code-bot bot commented Oct 19, 2025

Improve Rust Client Error Handling: Parse and Return Structured API Errors

This pull request enhances error handling in the Rust client for Chroma by introducing logic to parse structured API error responses and return them to the user rather than discarding them. It introduces a new ErrorResponse struct, utilizes this struct to extract and report error information including HTTP status codes, and updates error-handling paths in ChromaClientError and related client code. The changes also add targeted tests to verify correct reporting of API errors when duplicate collections are created.

Key Changes

• Added new ErrorResponse struct to rust/api-types/src/error.rs for API error payloads
• Re-exported ErrorResponse in rust/api-types/src/lib.rs for client use
• Extended ChromaClientError enum in rust/chroma/src/client/chroma_client.rs to include ApiError variant carrying error string and reqwest::StatusCode
• Refactored error-handling logic in the send method to attempt parsing error responses as JSON (ErrorResponse); non-JSON errors are returned with raw text and status code
• Added comprehensive test (test_live_cloud_parses_error) for verifying that duplicate collection creation returns a properly parsed structured error

Affected Areas

rust/chroma/src/client/chroma_client.rs
rust/api-types/src/error.rs
rust/api-types/src/lib.rs

This summary was automatically generated by @propel-code-bot

pub enum ChromaClientError {
#[error("Request error: {0:?}")]
RequestError(#[from] reqwest::Error),
#[error("API error: {0:?} ({1})")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

Using {0:?} in the thiserror message format will add quotes around the error string when displayed. Using {0} will display the string directly, which is usually more desirable for error messages.

Context for Agents
[**BestPractice**]

Using `{0:?}` in the `thiserror` message format will add quotes around the error string when displayed. Using `{0}` will display the string directly, which is usually more desirable for error messages.

File: rust/chroma/src/client/chroma_client.rs
Line: 28

Comment on lines 399 to 437
if let Some(response) = maybe_response {
let status = response.status();
let json = response.json::<serde_json::Value>().await?;

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!(
url = %url,
method =? method,
"Received response: {}",
serde_json::to_string_pretty(&json).unwrap_or_else(|_| "<failed to serialize>".to_string())
);
}

if let Ok(api_error) = serde_json::from_value::<ErrorResponse>(json) {
return Err(ChromaClientError::ApiError(
format!("{}: {}", api_error.error, api_error.message),
status,
));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

The logic here for deserializing and logging the JSON response is very similar to the logic in the success path that follows this match block. To avoid code duplication and improve maintainability, consider extracting this into a private helper function.

For example, a helper like this could serve both paths:

async fn deserialize_with_logging<T: serde::de::DeserializeOwned>(
    response: reqwest::Response,
    url: &reqwest::Url,
    method: &reqwest::Method,
) -> Result<T, ChromaClientError> {
    let json = response.json::<serde_json::Value>().await?;
    if tracing::enabled!(tracing::Level::TRACE) {
        tracing::trace!(
            url = %url,
            method =? method,
            "Received response: {}",
            serde_json::to_string_pretty(&json).unwrap_or_else(|_| "<failed to serialize>".to_string())
        );
    }
    serde_json::from_value::<T>(json).map_err(Into::into)
}

This would allow you to simplify both the error-handling and success paths significantly.

Context for Agents
[**BestPractice**]

The logic here for deserializing and logging the JSON response is very similar to the logic in the success path that follows this `match` block. To avoid code duplication and improve maintainability, consider extracting this into a private helper function.

For example, a helper like this could serve both paths:
```rust
async fn deserialize_with_logging<T: serde::de::DeserializeOwned>(
    response: reqwest::Response,
    url: &reqwest::Url,
    method: &reqwest::Method,
) -> Result<T, ChromaClientError> {
    let json = response.json::<serde_json::Value>().await?;
    if tracing::enabled!(tracing::Level::TRACE) {
        tracing::trace!(
            url = %url,
            method =? method,
            "Received response: {}",
            serde_json::to_string_pretty(&json).unwrap_or_else(|_| "<failed to serialize>".to_string())
        );
    }
    serde_json::from_value::<T>(json).map_err(Into::into)
}
```
This would allow you to simplify both the error-handling and success paths significantly.

File: rust/chroma/src/client/chroma_client.rs
Line: 418

Copy link
Contributor

@rescrv rescrv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not requesting changes, but I think it needs more work.

@codetheweb codetheweb force-pushed the feat-chroma-rust-client-create-list-collections branch from 703a271 to 0e9ef4c Compare October 20, 2025 00:10
@codetheweb codetheweb force-pushed the feat-chroma-rust-client-custom-error branch from e4f70b0 to c14bd60 Compare October 20, 2025 00:10
@codetheweb codetheweb requested a review from rescrv October 20, 2025 00:10
Copy link
Contributor

@rescrv rescrv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor Author

codetheweb commented Oct 20, 2025

Merge activity

  • Oct 20, 2:45 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Oct 20, 2:47 AM UTC: Graphite rebased this pull request as part of a merge.
  • Oct 20, 2:47 AM UTC: @codetheweb merged this pull request with Graphite.

@codetheweb codetheweb changed the base branch from feat-chroma-rust-client-create-list-collections to graphite-base/5656 October 20, 2025 02:45
@codetheweb codetheweb changed the base branch from graphite-base/5656 to main October 20, 2025 02:45
@codetheweb codetheweb force-pushed the feat-chroma-rust-client-custom-error branch from c14bd60 to 8246ef4 Compare October 20, 2025 02:46
@codetheweb codetheweb merged commit 65c4185 into main Oct 20, 2025
4 checks passed
@codetheweb codetheweb deleted the feat-chroma-rust-client-custom-error branch October 20, 2025 02:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants