From 8eac52315b807b9addc81334eb02538777098d33 Mon Sep 17 00:00:00 2001 From: Rushmore Mushambi Date: Wed, 19 Apr 2023 10:46:37 +0200 Subject: [PATCH] Fix HTTP string errors deserialization (#1821) --- lib/src/api/engine/remote/http/mod.rs | 31 ++++++++++++++++++++++----- lib/tests/api.rs | 3 +++ lib/tests/api/mod.rs | 11 +++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/src/api/engine/remote/http/mod.rs b/lib/src/api/engine/remote/http/mod.rs index 4d9f89f2..60c91841 100644 --- a/lib/src/api/engine/remote/http/mod.rs +++ b/lib/src/api/engine/remote/http/mod.rs @@ -179,11 +179,32 @@ async fn query(request: RequestBuilder) -> Result { info!(target: LOG, "{request:?}"); let response = request.send().await?.error_for_status()?; let bytes = response.bytes().await?; - let responses: Vec = - bung::from_slice(&bytes).map_err(|error| Error::ResponseFromBinary { - binary: bytes.to_vec(), - error, - })?; + let responses = match bung::from_slice::>(&bytes) { + Ok(responses) => responses, + Err(_) => { + let vec = + bung::from_slice::>(&bytes).map_err(|error| { + Error::ResponseFromBinary { + binary: bytes.to_vec(), + error, + } + })?; + let mut responses = Vec::with_capacity(vec.len()); + for (time, status, data) in vec { + let (result, detail) = match status { + Status::Ok => (Value::from(data), String::new()), + Status::Err => (Value::None, data), + }; + responses.push(HttpQueryResponse { + time, + status, + result, + detail, + }); + } + responses + } + }; let mut map = IndexMap::::with_capacity(responses.len()); for (index, response) in responses.into_iter().enumerate() { match response.status { diff --git a/lib/tests/api.rs b/lib/tests/api.rs index ecefc495..b039491e 100644 --- a/lib/tests/api.rs +++ b/lib/tests/api.rs @@ -5,6 +5,8 @@ mod api_integration { use serde_json::json; use std::borrow::Cow; use std::ops::Bound; + use surrealdb::error::Api as ApiError; + use surrealdb::error::Db as DbError; use surrealdb::opt::auth::Database; use surrealdb::opt::auth::Jwt; use surrealdb::opt::auth::Namespace; @@ -16,6 +18,7 @@ mod api_integration { use surrealdb::sql::statements::CommitStatement; use surrealdb::sql::thing; use surrealdb::sql::Thing; + use surrealdb::Error; use surrealdb::Surreal; use ulid::Ulid; diff --git a/lib/tests/api/mod.rs b/lib/tests/api/mod.rs index 7d2d45e1..e72d92e6 100644 --- a/lib/tests/api/mod.rs +++ b/lib/tests/api/mod.rs @@ -9,7 +9,16 @@ async fn connect() { #[tokio::test] async fn yuse() { let db = new_db().await; - db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap(); + let item = Ulid::new().to_string(); + let error = db.create::>(item.as_str()).await.unwrap_err(); + match error { + // Local engines return this error + Error::Db(DbError::NsEmpty) => {} + // Remote engines return this error + Error::Api(ApiError::Query(error)) if error.contains("Specify a namespace to use") => {} + error => panic!("{:?}", error), + } + db.use_ns(NS).use_db(item).await.unwrap(); } #[tokio::test]