Fix HTTP string errors deserialization (#1821)

This commit is contained in:
Rushmore Mushambi 2023-04-19 10:46:37 +02:00 committed by GitHub
parent a35ed83c88
commit 8eac52315b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 6 deletions

View file

@ -179,11 +179,32 @@ async fn query(request: RequestBuilder) -> Result<QueryResponse> {
info!(target: LOG, "{request:?}"); info!(target: LOG, "{request:?}");
let response = request.send().await?.error_for_status()?; let response = request.send().await?.error_for_status()?;
let bytes = response.bytes().await?; let bytes = response.bytes().await?;
let responses: Vec<HttpQueryResponse> = let responses = match bung::from_slice::<Vec<HttpQueryResponse>>(&bytes) {
bung::from_slice(&bytes).map_err(|error| Error::ResponseFromBinary { Ok(responses) => responses,
Err(_) => {
let vec =
bung::from_slice::<Vec<(String, Status, String)>>(&bytes).map_err(|error| {
Error::ResponseFromBinary {
binary: bytes.to_vec(), binary: bytes.to_vec(),
error, 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::<usize, QueryResult>::with_capacity(responses.len()); let mut map = IndexMap::<usize, QueryResult>::with_capacity(responses.len());
for (index, response) in responses.into_iter().enumerate() { for (index, response) in responses.into_iter().enumerate() {
match response.status { match response.status {

View file

@ -5,6 +5,8 @@ mod api_integration {
use serde_json::json; use serde_json::json;
use std::borrow::Cow; use std::borrow::Cow;
use std::ops::Bound; 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::Database;
use surrealdb::opt::auth::Jwt; use surrealdb::opt::auth::Jwt;
use surrealdb::opt::auth::Namespace; use surrealdb::opt::auth::Namespace;
@ -16,6 +18,7 @@ mod api_integration {
use surrealdb::sql::statements::CommitStatement; use surrealdb::sql::statements::CommitStatement;
use surrealdb::sql::thing; use surrealdb::sql::thing;
use surrealdb::sql::Thing; use surrealdb::sql::Thing;
use surrealdb::Error;
use surrealdb::Surreal; use surrealdb::Surreal;
use ulid::Ulid; use ulid::Ulid;

View file

@ -9,7 +9,16 @@ async fn connect() {
#[tokio::test] #[tokio::test]
async fn yuse() { async fn yuse() {
let db = new_db().await; 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::<Vec<()>>(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] #[tokio::test]