1use 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#[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 #[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}