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