Skip to content

Commit

Permalink
refactor error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
ZENOTME committed Jul 13, 2022
1 parent 6c99445 commit 2f013a2
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 164 deletions.
132 changes: 112 additions & 20 deletions src/utils/pgwire/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,133 @@
use std::io::Error as IoError;

use thiserror::Error;
pub type Result<T> = std::result::Result<T, PsqlError>;
pub type PsqlResult<T> = std::result::Result<T, PsqlError>;
use crate::pg_server::BoxedError;

/// Error type used in pgwire crates.
#[derive(Error, Debug)]
pub enum PsqlError {
#[error("Encode error {0}.")]
CancelError(String),
#[error("SslError: {0}.")]
SslError(#[from] SslError),

#[error("{0}")]
IoError(#[from] IoError),
#[error("StartupError: {0}.")]
StartupError(#[from] StartupError),

#[error("Failed to handle ssl request: {0}")]
SslError(IoError),
#[error("PasswordError: {0}.")]
PasswordError(#[from] PasswordError),

#[error("Invaild sql: {0}")]
InvaildSQL(IoError),
#[error("QueryError: {0}.")]
QueryError(#[from] QueryError),

#[error("Failed to get response from session: {0}")]
ReponseError(BoxedError),
#[error("CancelError: {0}.")]
CancelError(#[from] CancelError),

// The difference between IoError and ReadMsgIoError is that ReadMsgIoError needed to report
// to users but IoError does not.
#[error("Fail to read message: {0}")]
ReadMsgError(IoError),
#[error("ParseError: {0}.")]
ParseError(#[from] ParseError),

#[error("BindError: {0}.")]
BindError(#[from] BindError),

#[error("Fail to set up pg session: {0}")]
StartupError(IoError),
#[error("ExecuteError: {0}.")]
ExecuteError(#[from] ExecuteError),

#[error("DescribeErrro: {0}.")]
DescribeError(#[from] DescribeError),

#[error("CloseError: {0}.")]
CloseError(#[from] CloseError),

#[error("ReadMsgError: {0}.")]
ReadMsgError(IoError),

#[error("Failed to authenticate session: {0}.")]
AuthenticationError(IoError),
// Use for error occurs when sending error msg.
#[error("{0}")]
IoError(#[from] IoError),
}

impl PsqlError {
/// Construct a Cancel error. Used when Ctrl-c a processing query. Similar to PG.
pub fn cancel() -> Self {
PsqlError::CancelError("ERROR: canceling statement due to user request".to_string())
PsqlError::CancelError(CancelError::Content(
"ERROR: canceling statement due to user request".to_string(),
))
}
}

#[derive(Error, Debug)]
pub enum SslError {
#[error("{0}")]
IoError(#[from] IoError),
}

#[derive(Error, Debug)]
pub enum StartupError {
#[error("{0}")]
IoError(#[from] IoError),

#[error("connect error:{0}")]
ConnectError(BoxedError),
}

#[derive(Error, Debug)]
pub enum PasswordError {
#[error("{0}")]
IoError(#[from] IoError),

#[error("Invalid password")]
InvalidPassword,
}

#[derive(Error, Debug)]
pub enum CancelError {
#[error("{0}")]
Content(String),

#[error("{0}")]
IoError(#[from] IoError),
}

#[derive(Error, Debug)]
pub enum QueryError {
#[error("{0}")]
IoError(#[from] IoError),

#[error("Response error from frontend: {0}")]
ResponseError(BoxedError),
}

#[derive(Error, Debug)]
pub enum ParseError {
#[error("{0}")]
IoError(#[from] IoError),

#[error("Response error from frontend: {0}")]
ResponseError(BoxedError),
}

#[derive(Error, Debug)]
pub enum BindError {
#[error("{0}")]
IoError(#[from] IoError),
}

#[derive(Error, Debug)]
pub enum ExecuteError {
#[error("{0}")]
IoError(#[from] IoError),

#[error("Response error from frontend: {0}")]
ResponseError(BoxedError),
}

#[derive(Error, Debug)]
pub enum DescribeError {
#[error("{0}")]
IoError(#[from] IoError),
}

#[derive(Error, Debug)]
pub enum CloseError {
#[error("{0}")]
IoError(#[from] IoError),
}
Loading

0 comments on commit 2f013a2

Please sign in to comment.