Skip to main content

pgwire/
error.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::fmt;
16use std::io::Error as IoError;
17
18use risingwave_common::error::code::PostgresErrorCode;
19use thiserror::Error;
20
21use crate::pg_server::BoxedError;
22pub type PsqlResult<T> = std::result::Result<T, PsqlError>;
23
24/// Error type used in pgwire crates.
25#[derive(Error, Debug)]
26pub enum PsqlError {
27    #[error("Failed to start a new session: {0}")]
28    StartupError(
29        #[source]
30        #[backtrace]
31        BoxedError,
32    ),
33
34    #[error("Invalid password")]
35    PasswordError,
36
37    #[error("Protocol violation: {0}")]
38    ProtocolError(
39        #[source]
40        #[backtrace]
41        ProtocolViolationError,
42    ),
43
44    #[error("Failed to run the query: {0}")]
45    SimpleQueryError(
46        #[source]
47        #[backtrace]
48        BoxedError,
49    ),
50
51    #[error("Failed to prepare the statement: {0}")]
52    ExtendedPrepareError(
53        #[source]
54        #[backtrace]
55        BoxedError,
56    ),
57
58    #[error("Failed to execute the statement: {0}")]
59    ExtendedExecuteError(
60        #[source]
61        #[backtrace]
62        BoxedError,
63    ),
64
65    #[error(transparent)]
66    IoError(#[from] IoError),
67
68    /// Uncategorized error for describe, bind.
69    #[error(transparent)]
70    Uncategorized(
71        #[from]
72        #[backtrace]
73        BoxedError,
74    ),
75
76    #[error("Panicked when handling the request: {0}
77This is a bug. We would appreciate a bug report at:
78  https://github.com/risingwavelabs/risingwave/issues/new?labels=type%2Fbug&template=bug_report.yml")]
79    Panic(String),
80
81    #[error("Unable to setup an SSL connection")]
82    SslError(#[from] openssl::ssl::Error),
83
84    #[error("terminating connection due to idle-in-transaction timeout")]
85    IdleInTxnTimeout,
86
87    #[error("Server throttled: {0}")]
88    ServerThrottle(String),
89}
90
91#[derive(Debug)]
92pub struct ProtocolViolationError(String);
93
94impl fmt::Display for ProtocolViolationError {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        self.0.fmt(f)
97    }
98}
99
100impl std::error::Error for ProtocolViolationError {
101    fn provide<'a>(&'a self, request: &mut std::error::Request<'a>) {
102        request.provide_value(PostgresErrorCode::ProtocolViolation);
103    }
104}
105
106impl PsqlError {
107    pub fn protocol_error(message: impl Into<String>) -> Self {
108        Self::ProtocolError(ProtocolViolationError(message.into()))
109    }
110
111    pub fn no_statement() -> Self {
112        PsqlError::Uncategorized("No statement found".into())
113    }
114
115    pub fn no_portal() -> Self {
116        PsqlError::Uncategorized("No portal found".into())
117    }
118}