refactor: adjust outermost error message (#859)

* refactor: adjust outermost error message

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* fix clippy

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* preserve tonic status code

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2023-01-10 17:28:27 +08:00
committed by GitHub
parent 5fb417ec7c
commit 32d51947a4
9 changed files with 49 additions and 24 deletions

View File

@@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::str::FromStr;
use api::v1::ddl_request::Expr as DdlExpr;
use api::v1::greptime_request::Request;
use api::v1::query_request::Query;
@@ -113,15 +115,17 @@ impl Database {
.await
.map_err(|e| {
let code = get_metadata_value(&e, INNER_ERROR_CODE)
.and_then(|s| s.parse::<u32>().ok())
.unwrap_or(e.code() as u32);
let err_msg = get_metadata_value(&e, INNER_ERROR_MSG).unwrap_or(e.to_string());
error::FlightGetSnafu {
addr: client.addr(),
code,
err_msg,
}
.build()
.and_then(|s| StatusCode::from_str(&s).ok())
.unwrap_or(StatusCode::Unknown);
let msg = get_metadata_value(&e, INNER_ERROR_MSG).unwrap_or(e.to_string());
error::ExternalSnafu { code, msg }
.fail::<()>()
.map_err(BoxedError::new)
.context(error::FlightGetSnafu {
tonic_code: e.code(),
addr: client.addr(),
})
.unwrap_err()
})?;
let decoder = &mut FlightDecoder::default();

View File

@@ -15,6 +15,7 @@
use std::any::Any;
use common_error::prelude::*;
use tonic::Code;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
@@ -26,16 +27,15 @@ pub enum Error {
},
#[snafu(display(
"Failed to do Flight get, addr: {}, code: {}, err_msg: {}",
"Failed to do Flight get, addr: {}, code: {}, source: {}",
addr,
code,
err_msg
tonic_code,
source
))]
FlightGet {
addr: String,
code: u32,
err_msg: String,
backtrace: Backtrace,
tonic_code: Code,
source: BoxedError,
},
#[snafu(display("Failed to convert FlightData, source: {}", source))]
@@ -69,6 +69,10 @@ pub enum Error {
#[snafu(backtrace)]
source: common_grpc::error::Error,
},
/// Error deserialized from gRPC metadata
#[snafu(display("{}", msg))]
ExternalError { code: StatusCode, msg: String },
}
pub type Result<T> = std::result::Result<T, Error>;
@@ -77,13 +81,14 @@ impl ErrorExt for Error {
fn status_code(&self) -> StatusCode {
match self {
Error::IllegalFlightMessages { .. }
| Error::FlightGet { .. }
| Error::ColumnDataType { .. }
| Error::MissingField { .. } => StatusCode::Internal,
Error::FlightGet { source, .. } => source.status_code(),
Error::CreateChannel { source, .. } | Error::ConvertFlightData { source } => {
source.status_code()
}
Error::IllegalGrpcClientState { .. } => StatusCode::Unexpected,
Error::ExternalError { code, .. } => *code,
}
}