Skip to content

Cosmos: Fix #2824 by forcing 'enable_content_response_on_write' on for read item #2894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,11 @@ impl ContainerClient {
item_id: &str,
options: Option<ItemOptions<'_>>,
) -> azure_core::Result<Response<T>> {
let options = options.unwrap_or_default();
let mut options = options.unwrap_or_default();

// Read APIs should always return the item, ignoring whatever the user set.
options.enable_content_response_on_write = true;

let link = self.items_link.item(item_id);
let url = self.pipeline.url(&link);
let mut req = Request::new(url, Method::Get);
Expand Down
53 changes: 46 additions & 7 deletions sdk/cosmos/azure_data_cosmos/src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ mod signature_target;
use std::sync::Arc;

pub use authorization_policy::AuthorizationPolicy;
use azure_core::http::{
request::{options::ContentType, Request},
response::Response,
ClientOptions, Context, Method, PagerState, RawResponse,
use azure_core::{
error::HttpError,
http::{
request::{options::ContentType, Request},
response::Response,
ClientOptions, Context, Method, PagerState, RawResponse,
},
};
use futures::TryStreamExt;
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -64,7 +67,7 @@ impl CosmosPipeline {
resource_link: ResourceLink,
) -> azure_core::Result<RawResponse> {
let ctx = ctx.with_value(resource_link);
self.pipeline.send(&ctx, request).await
self.pipeline.send(&ctx, request).await?.success().await
}

pub async fn send<T>(
Expand All @@ -75,7 +78,7 @@ impl CosmosPipeline {
) -> azure_core::Result<Response<T>> {
self.send_raw(ctx, request, resource_link)
.await
.map(|r| r.into())
.map(Into::into)
}

pub fn send_query_request<T: DeserializeOwned + Send>(
Expand Down Expand Up @@ -105,7 +108,7 @@ impl CosmosPipeline {
req.insert_header(constants::CONTINUATION, continuation);
}

let resp = pipeline.send(&ctx, &mut req).await?;
let resp = pipeline.send(&ctx, &mut req).await?.success().await?;
let page = FeedPage::<T>::from_response(resp).await?;

Ok(page.into())
Expand Down Expand Up @@ -191,3 +194,39 @@ pub(crate) fn create_base_query_request(url: Url, query: &Query) -> azure_core::
request.set_json(query)?;
Ok(request)
}

trait ResponseExt {
async fn success(self) -> azure_core::Result<Self>
where
Self: Sized;
}

impl<T, F> ResponseExt for Response<T, F> {
async fn success(self) -> azure_core::Result<Self> {
if self.status().is_success() {
Ok(self)
} else {
RawResponse::from(self).success().await.map(Into::into)
}
}
}

impl ResponseExt for RawResponse {
Copy link
Member

Choose a reason for hiding this comment

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

@chlowell this is a good idea we might consider in azure_core. Combined with #2725 we could also use something like this to wrap standard Azure errors. We'd have to implement it (foreign trait otherwise), but could also allow clients to pass in custom success codes or something, much like .NET does for their DPG clients and HTTP status code categorizations.

async fn success(self) -> azure_core::Result<Self> {
if self.status().is_success() {
Ok(self)
} else {
let http_error = HttpError::new(self).await;
let status = http_error.status();
let error_kind = azure_core::error::ErrorKind::http_response(
status,
http_error.error_code().map(ToOwned::to_owned),
);
Err(azure_core::Error::full(
error_kind,
http_error,
format!("Request failed with status code: {}", status),
))
}
}
}
Loading