From cb094aa80257bd50a27dd6477937ea67e0b0fa43 Mon Sep 17 00:00:00 2001 From: test Date: Mon, 29 Apr 2024 21:43:53 +0800 Subject: [PATCH] apply review comments and clean code --- src/cmd/src/cli/export.rs | 26 ++++++++++++++------------ src/cmd/src/error.rs | 8 ++++---- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/cmd/src/cli/export.rs b/src/cmd/src/cli/export.rs index 79bd8630169a..70ca80d11db3 100644 --- a/src/cmd/src/cli/export.rs +++ b/src/cmd/src/cli/export.rs @@ -17,7 +17,7 @@ use std::sync::Arc; use async_trait::async_trait; use base64::engine::general_purpose; -use base64::Engine as _; +use base64::Engine; use clap::{Parser, ValueEnum}; use client::DEFAULT_SCHEMA_NAME; use common_telemetry::{debug, error, info, warn}; @@ -121,29 +121,31 @@ impl Export { sql ); - let client = reqwest::Client::new(); - let mut request = client + let mut request = reqwest::Client::new() .get(&url) .header("Content-Type", "application/x-www-form-urlencoded"); if let Some(ref auth) = self.auth_header { request = request.header("Authorization", auth); } - let response = request - .send() - .await - .with_context(|_| HttpQuerySqlSnafu { reason: url })?; - snafu::ensure!(response.status() == 200, EmptyResultSnafu); + let response = request.send().await.with_context(|_| HttpQuerySqlSnafu { + reason: format!("bad url: {}", url), + })?; + let response = response + .error_for_status() + .with_context(|_| HttpQuerySqlSnafu { + reason: format!("query failed: {}", sql), + })?; let text = response.text().await.with_context(|_| HttpQuerySqlSnafu { reason: "cannot get response text".to_string(), })?; let body = serde_json::from_str::(&text).context(SerdeJsonSnafu)?; - match &body.output()[0] { - GreptimeQueryOutput::Records(records) => Ok(Some(records.rows().clone())), - GreptimeQueryOutput::AffectedRows(_) => Ok(None), - } + Ok(body.output().first().and_then(|output| match output { + GreptimeQueryOutput::Records(records) => Some(records.rows().clone()), + GreptimeQueryOutput::AffectedRows(_) => None, + })) } /// Iterate over all db names. diff --git a/src/cmd/src/error.rs b/src/cmd/src/error.rs index 8739190f51dd..d24f00f5be44 100644 --- a/src/cmd/src/error.rs +++ b/src/cmd/src/error.rs @@ -291,7 +291,9 @@ impl ErrorExt for Error { Error::StartProcedureManager { source, .. } | Error::StopProcedureManager { source, .. } => source.status_code(), Error::StartWalOptionsAllocator { source, .. } => source.status_code(), - Error::ReplCreation { .. } | Error::Readline { .. } => StatusCode::Internal, + Error::ReplCreation { .. } | Error::Readline { .. } | Error::HttpQuerySql { .. } => { + StatusCode::Internal + } Error::CollectRecordBatches { source, .. } | Error::PrettyPrintRecordBatches { source, .. } => source.status_code(), Error::StartMetaClient { source, .. } => source.status_code(), @@ -301,9 +303,7 @@ impl ErrorExt for Error { Error::SubstraitEncodeLogicalPlan { source, .. } => source.status_code(), Error::StartCatalogManager { source, .. } => source.status_code(), - Error::SerdeJson { .. } | Error::FileIo { .. } | Error::HttpQuerySql { .. } => { - StatusCode::Unexpected - } + Error::SerdeJson { .. } | Error::FileIo { .. } => StatusCode::Unexpected, Error::Other { source, .. } => source.status_code(),