diff --git a/core/src/databases/database.rs b/core/src/databases/database.rs index f8ecb55089dd..b3fb2070c905 100644 --- a/core/src/databases/database.rs +++ b/core/src/databases/database.rs @@ -41,7 +41,7 @@ pub enum QueryDatabaseError { impl From for QueryDatabaseError { fn from(e: SqliteWorkerError) -> Self { match &e { - SqliteWorkerError::ServerError(code, _) => match code.as_str() { + SqliteWorkerError::ServerError(_, code, _) => match code.as_str() { "too_many_result_rows" => QueryDatabaseError::TooManyResultRows, _ => e.into(), }, diff --git a/core/src/sqlite_workers/client.rs b/core/src/sqlite_workers/client.rs index 56630c9a5f99..151ba1a5aa83 100644 --- a/core/src/sqlite_workers/client.rs +++ b/core/src/sqlite_workers/client.rs @@ -20,12 +20,12 @@ pub struct SqliteWorker { #[derive(Debug, Error)] pub enum SqliteWorkerError { - #[error("SqliteWorkerError Server error (code={0}, status={1})")] - ServerError(String, u16), - #[error("SqliteWorkerError Unexpected server error (status={0}): {1}")] - UnexpectedServerError(u16, String), - #[error("SqliteWorkerError Body parsing error (status={1}): {0}")] - BodyParsingError(serde_json::Error, u16), + #[error("SqliteWorkerError Server error (uri={0}, code={1}, status={2})")] + ServerError(String, String, u16), + #[error("SqliteWorkerError Unexpected server error (uri={0}, status={1}): {2}")] + UnexpectedServerError(String, u16, String), + #[error("SqliteWorkerError Body parsing error (uri={1}, status={2}): {0}")] + BodyParsingError(serde_json::Error, String, u16), #[error("SqliteWorkerError HyperError: {0}")] HyperError(#[from] hyper::Error), #[error("SqliteWorkerError Unexpected error: {0}")] @@ -87,9 +87,7 @@ impl SqliteWorker { .to_string(), ))?; - let res = Client::new().request(req).await?; - - let body_bytes = get_response_body(res).await?; + let body_bytes = get_response_body(req).await?; #[derive(Deserialize)] struct ExecuteQueryResponseBody { @@ -121,9 +119,7 @@ impl SqliteWorker { .uri(format!("{}/databases/{}", worker_url, database_unique_id)) .body(Body::from(""))?; - let res = Client::new().request(req).await?; - - let _ = get_response_body(res).await?; + let _ = get_response_body(req).await?; Ok(()) } @@ -136,14 +132,16 @@ impl SqliteWorker { .uri(format!("{}/databases", worker_url)) .body(Body::from(""))?; - let res = Client::new().request(req).await?; - let _ = get_response_body(res).await?; + let _ = get_response_body(req).await?; Ok(()) } } -async fn get_response_body(res: hyper::Response) -> Result { +async fn get_response_body(req: hyper::Request) -> Result { + let uri = req.uri().to_string(); + let res = Client::new().request(req).await?; + let status = res.status().as_u16(); let body = hyper::body::to_bytes(res.into_body()).await?; @@ -151,7 +149,8 @@ async fn get_response_body(res: hyper::Response) -> Result Ok(body), s => { let body_json: serde_json::Value = serde_json::from_slice(&body) - .map_err(|e| SqliteWorkerError::BodyParsingError(e, s))?; + .map_err(|e| SqliteWorkerError::BodyParsingError(e, uri.clone(), s))?; + let error = body_json.get("error"); let error_code = match error { Some(e) => e @@ -162,8 +161,9 @@ async fn get_response_body(res: hyper::Response) -> Result None, }; match error_code { - Some(code) => Err(SqliteWorkerError::ServerError(code.to_string(), s))?, + Some(code) => Err(SqliteWorkerError::ServerError(uri, code.to_string(), s))?, None => Err(SqliteWorkerError::UnexpectedServerError( + uri, s, format!("No error code in response: {}", body_json.to_string()), ))?,