pgwire/
error_or_notice.rs

1// Copyright 2022 RisingWave Labs
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 std::borrow::Cow;
16
17use risingwave_common::error::code::PostgresErrorCode;
18use risingwave_common::error::error_request_copy;
19use thiserror_ext::AsReport;
20
21/// ErrorOrNoticeMessage defines messages that can appear in ErrorResponse and NoticeResponse.
22pub struct ErrorOrNoticeMessage<'a> {
23    pub severity: Severity,
24    pub error_code: PostgresErrorCode,
25    pub message: Cow<'a, str>,
26}
27
28impl<'a> ErrorOrNoticeMessage<'a> {
29    /// Create a Postgres error message from an error, with the error code and message extracted from the error.
30    pub fn error(error: &(dyn std::error::Error + 'static), pretty: bool) -> Self {
31        Self::error_with_severity(error, pretty, Severity::Error)
32    }
33
34    /// Create a Postgres error message from an error, with a custom severity.
35    pub fn error_with_severity(
36        error: &(dyn std::error::Error + 'static),
37        pretty: bool,
38        severity: Severity,
39    ) -> Self {
40        let message = if pretty {
41            error.to_report_string_pretty()
42        } else {
43            error.to_report_string()
44        };
45        let error_code = error_request_copy::<PostgresErrorCode>(error)
46            .filter(|e| e.is_error()) // should not be warning or success
47            .unwrap_or(PostgresErrorCode::InternalError);
48
49        Self {
50            severity,
51            error_code,
52            message: Cow::Owned(message),
53        }
54    }
55
56    /// Create a Postgres notice message from a string.
57    pub fn notice(message: &'a str) -> Self {
58        Self {
59            severity: Severity::Notice,
60            error_code: PostgresErrorCode::SuccessfulCompletion,
61            message: Cow::Borrowed(message),
62        }
63    }
64}
65
66/// Severity: the field contents are ERROR, FATAL, or PANIC (in an error message), or WARNING,
67/// NOTICE, DEBUG, INFO, or LOG (in a notice message), or a localized translation of one of these.
68/// Always present.
69#[derive(PartialEq, Eq, Clone, Copy, Debug)]
70pub enum Severity {
71    Error,
72    Fatal,
73    Panic,
74    Notice,
75    Warning,
76    Debug,
77    Log,
78    Info,
79}
80
81impl Severity {
82    pub fn as_str(&self) -> &str {
83        match self {
84            Severity::Error => "ERROR",
85            Severity::Fatal => "FATAL",
86            Severity::Panic => "PANIC",
87            Severity::Notice => "NOTICE",
88            Severity::Warning => "WARNING",
89            Severity::Debug => "DEBUG",
90            Severity::Log => "LOG",
91            Severity::Info => "INFO",
92        }
93    }
94}