servers/postgres/utils.rs
1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use common_error::ext::ErrorExt;
16use common_telemetry::{debug, error};
17use pgwire::error::PgWireError;
18
19use crate::postgres::types::PgErrorCode;
20
21pub fn convert_err(e: impl ErrorExt) -> PgWireError {
22 let status_code = e.status_code();
23 if status_code.should_log_error() {
24 let root_error = e.root_cause().unwrap_or(&e);
25 error!(e; "Failed to handle postgres query, code: {}, error: {}", status_code, root_error.to_string());
26 } else {
27 debug!(
28 "Failed to handle postgres query, code: {}, error: {:?}",
29 status_code, e
30 );
31 }
32
33 PgWireError::UserError(Box::new(
34 PgErrorCode::from(status_code).to_err_info(e.output_msg()),
35 ))
36}