diff --git a/src/common/error/src/ext.rs b/src/common/error/src/ext.rs index 271310e80f96..3b4d15a8350d 100644 --- a/src/common/error/src/ext.rs +++ b/src/common/error/src/ext.rs @@ -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 { diff --git a/src/servers/src/mysql/writer.rs b/src/servers/src/mysql/writer.rs index fac8b7381086..811dc9447815 100644 --- a/src/servers/src/mysql/writer.rs +++ b/src/servers/src/mysql/writer.rs @@ -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(); diff --git a/src/servers/src/postgres/handler.rs b/src/servers/src/postgres/handler.rs index 4be95374eacd..75720251bd30 100644 --- a/src/servers/src/postgres/handler.rs +++ b/src/servers/src/postgres/handler.rs @@ -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}; @@ -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(), + )))) + } } }