fix: display codes in metasrv client errors (#8558)

* fix: display codes in metasrv client errors

Signed-off-by: evenyag <realevenyag@gmail.com>

* fix: normalize meta client errors in sqlness

Signed-off-by: evenyag <realevenyag@gmail.com>

---------

Signed-off-by: evenyag <realevenyag@gmail.com>
This commit is contained in:
Yingwen
2026-07-20 14:48:49 +08:00
committed by GitHub
parent abcbd4f934
commit b1580c98f6
2 changed files with 62 additions and 1 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ pub enum Error {
location: Location,
},
#[snafu(display("{}", msg))]
#[snafu(display("{}, code: {}, tonic code: {}", msg, code, tonic_code))]
MetaServer {
code: StatusCode,
msg: String,
+61
View File
@@ -40,6 +40,7 @@ impl<E: ErrorExt> Display for ErrorFormatter<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let status_code = self.0.status_code();
let root_cause = self.0.output_msg();
let root_cause = normalize_meta_client_error(&root_cause, status_code);
write!(
f,
"Error: {}({status_code}), {root_cause}",
@@ -48,6 +49,17 @@ impl<E: ErrorExt> Display for ErrorFormatter<E> {
}
}
fn normalize_meta_client_error(
root_cause: &str,
status_code: common_error::status_code::StatusCode,
) -> &str {
let details_prefix = format!(", code: {status_code}, tonic code: ");
match root_cause.rsplit_once(&details_prefix) {
Some((message, tonic_code)) if !tonic_code.is_empty() => message,
_ => root_cause,
}
}
/// A formatter for [`Output`].
pub struct OutputFormatter(Output);
@@ -214,3 +226,52 @@ pub fn build_recordbatches_from_mysql_rows(rows: &[MySqlRow]) -> RecordBatches {
RecordBatches::try_from_columns(schema, columns)
.expect("Failed to construct recordbatches from columns. Please check the schema.")
}
#[cfg(test)]
mod tests {
use common_error::ext::PlainError;
use common_error::status_code::StatusCode;
use super::*;
#[test]
fn test_normalize_meta_client_error() {
let error = PlainError::new(
"Invalid options, code: InvalidArguments, tonic code: Client specified an invalid argument"
.to_string(),
StatusCode::InvalidArguments,
);
assert_eq!(
"Error: 1004(InvalidArguments), Invalid options",
ErrorFormatter::from(error).to_string()
);
}
#[test]
fn test_preserve_regular_error() {
let error = PlainError::new(
"Invalid options without transport details".to_string(),
StatusCode::InvalidArguments,
);
assert_eq!(
"Error: 1004(InvalidArguments), Invalid options without transport details",
ErrorFormatter::from(error).to_string()
);
}
#[test]
fn test_preserve_error_with_mismatched_code() {
let error = PlainError::new(
"Invalid options, code: Unsupported, tonic code: Operation is not supported"
.to_string(),
StatusCode::InvalidArguments,
);
assert_eq!(
"Error: 1004(InvalidArguments), Invalid options, code: Unsupported, tonic code: Operation is not supported",
ErrorFormatter::from(error).to_string()
);
}
}