refactor(proto): replace anyhow with thiserror in handshake
This commit is contained in:
parent
da6410ce05
commit
165f871324
3 changed files with 25 additions and 2 deletions
21
proto/src/handshake/errors.rs
Normal file
21
proto/src/handshake/errors.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
use thiserror::Error;
|
||||||
|
use tokio::io;
|
||||||
|
use crate::message::errors::ProtoDeserializeError;
|
||||||
|
use crate::reader::errors::{ProtoConsumeError, ProtoPeekError};
|
||||||
|
use crate::writer::errors::ProtoWriteError;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum ServerHandshakeError {
|
||||||
|
#[error("startup message not found")]
|
||||||
|
MissingStartupMessage,
|
||||||
|
#[error("reading from socket failed")]
|
||||||
|
Io(#[from] io::Error),
|
||||||
|
#[error("deserialization of inner data failed")]
|
||||||
|
Deserialize(#[from] ProtoDeserializeError),
|
||||||
|
#[error("peeking special message failed")]
|
||||||
|
Peek(#[from] ProtoPeekError),
|
||||||
|
#[error("consuming special message failed")]
|
||||||
|
Consume(#[from] ProtoConsumeError),
|
||||||
|
#[error("writing message to socket failed")]
|
||||||
|
Write(#[from] ProtoWriteError),
|
||||||
|
}
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
pub mod errors;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::handshake::errors::ServerHandshakeError;
|
||||||
use crate::message::backend::{
|
use crate::message::backend::{
|
||||||
AuthenticationOkData, BackendKeyDataData, BackendMessage, ParameterStatusData,
|
AuthenticationOkData, BackendKeyDataData, BackendMessage, ParameterStatusData,
|
||||||
ReadyForQueryData,
|
ReadyForQueryData,
|
||||||
|
|
@ -13,7 +14,7 @@ pub async fn do_server_handshake(
|
||||||
name: &str,
|
name: &str,
|
||||||
process: i32,
|
process: i32,
|
||||||
secret: i32,
|
secret: i32,
|
||||||
) -> anyhow::Result<StartupMessageData> {
|
) -> Result<StartupMessageData, ServerHandshakeError> {
|
||||||
match &reader.peek_special_message().await? {
|
match &reader.peek_special_message().await? {
|
||||||
Some(msg @ SpecialMessage::SSLRequest) => {
|
Some(msg @ SpecialMessage::SSLRequest) => {
|
||||||
reader.consume_special_message(msg).await?;
|
reader.consume_special_message(msg).await?;
|
||||||
|
|
@ -31,7 +32,7 @@ pub async fn do_server_handshake(
|
||||||
data.clone()
|
data.clone()
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(anyhow::anyhow!("Expected Startup Message"));
|
return Err(ServerHandshakeError::MissingStartupMessage);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue