pgwire/
error_or_notice.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// ErrorOrNoticeMessage defines messages that can appear in ErrorResponse and NoticeResponse.
pub struct ErrorOrNoticeMessage<'a> {
    pub severity: Severity,
    pub state: SqlState,
    pub message: &'a str,
}

impl<'a> ErrorOrNoticeMessage<'a> {
    pub fn internal_error(message: &'a str) -> Self {
        Self {
            severity: Severity::Error,
            state: SqlState::INTERNAL_ERROR,
            message,
        }
    }

    pub fn notice(message: &'a str) -> Self {
        Self {
            severity: Severity::Notice,
            state: SqlState::SUCCESSFUL_COMPLETION,
            message,
        }
    }
}

/// Severity: the field contents are ERROR, FATAL, or PANIC (in an error message), or WARNING,
/// NOTICE, DEBUG, INFO, or LOG (in a notice message), or a localized translation of one of these.
/// Always present.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Severity {
    Error,
    Fatal,
    Panic,
    Notice,
    Warning,
    Debug,
    Log,
    Info,
}

impl Severity {
    pub fn as_str(&self) -> &str {
        match self {
            Severity::Error => "ERROR",
            Severity::Fatal => "FATAL",
            Severity::Panic => "PANIC",
            Severity::Notice => "NOTICE",
            Severity::Warning => "WARNING",
            Severity::Debug => "DEBUG",
            Severity::Log => "LOG",
            Severity::Info => "INFO",
        }
    }
}

/// Code: the SQLSTATE code for the error (see <https://www.postgresql.org/docs/current/errcodes-appendix.html>).
/// Not localizable. Always present.
#[derive(PartialEq, Eq, Clone, Debug)]
#[allow(clippy::upper_case_acronyms)]
pub enum Code {
    E00000,
    E01000,
    EXX000,
}

/// SQLSTATE error code.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct SqlState(Code);

impl SqlState {
    /// Class XX — Internal Error
    pub const INTERNAL_ERROR: SqlState = SqlState(Code::EXX000);
    /// Class 00 — Successful Completion
    pub const SUCCESSFUL_COMPLETION: SqlState = SqlState(Code::E00000);
    /// Class 01 — Warning
    pub const WARNING: SqlState = SqlState(Code::E01000);

    pub fn code(&self) -> &str {
        match &self.0 {
            Code::E00000 => "00000",
            Code::E01000 => "01000",
            Code::EXX000 => "XX000",
        }
    }
}