Skip to content

Commit

Permalink
feat: show root cause on the error line
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Jul 27, 2024
1 parent 021ec7b commit 7bc97d7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/common/error/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ pub trait ErrorExt: StackError {
}
}
}

/// Find out root level error for nested error
fn root_cause(&self) -> Option<&dyn std::error::Error>
where
Self: Sized,
{
let error = self.last();
if let Some(external_error) = error.source() {
let external_root = external_error.sources().last().unwrap();
Some(external_root)
} else {
None
}
}
}

pub trait StackError: std::error::Error {
Expand Down
7 changes: 4 additions & 3 deletions src/servers/src/mysql/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ pub fn handle_err(e: impl ErrorExt) -> (ErrorKind, String) {
let kind = mysql_error_kind(&status_code);

if status_code.should_log_error() {
error!(e; "Failed to handle mysql query, code: {}, kind: {:?}", status_code, kind);
let root_error = e.root_cause().unwrap_or(&e);
error!(e; "Failed to handle mysql query, code: {}, error: {}", status_code, root_error.to_string());
} else {
debug!(
"Failed to handle mysql query, code: {}, kind: {:?}, error: {:?}",
status_code, kind, e
"Failed to handle mysql query, code: {}, error: {:?}",
status_code, e
);
};
let msg = e.output_msg();
Expand Down
25 changes: 19 additions & 6 deletions src/servers/src/postgres/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use common_error::ext::ErrorExt;
use common_query::{Output, OutputData};
use common_recordbatch::error::Result as RecordBatchResult;
use common_recordbatch::RecordBatch;
use common_telemetry::tracing;
use common_telemetry::{debug, error, tracing};
use datatypes::schema::SchemaRef;
use futures::{future, stream, Stream, StreamExt};
use pgwire::api::portal::{Format, Portal};
Expand Down Expand Up @@ -95,11 +95,24 @@ fn output_to_query_response<'a>(
)
}
},
Err(e) => Ok(Response::Error(Box::new(ErrorInfo::new(
"ERROR".to_string(),
"XX000".to_string(),
e.output_msg(),
)))),
Err(e) => {
let status_code = e.status_code();

if status_code.should_log_error() {
let root_error = e.root_cause().unwrap_or(&e);
error!(e; "Failed to handle postgres query, code: {}, error: {}", status_code, root_error.to_string());
} else {
debug!(
"Failed to handle postgres query, code: {}, error: {:?}",
status_code, e
);
};
Ok(Response::Error(Box::new(ErrorInfo::new(
"ERROR".to_string(),
"XX000".to_string(),
e.output_msg(),
))))
}
}
}

Expand Down

0 comments on commit 7bc97d7

Please sign in to comment.