use futures_util::SinkExt;
use tokio::net::TcpStream;
use tokio_util::codec::Framed;
use tracing::warn;
use crate::proto::backend::{BackendMessage, NoticeFields};
use crate::proto::codec::FrontendCodec;
use crate::proto::types::TransactionStatus;
pub async fn send_fatal(
client: &mut Framed<TcpStream, FrontendCodec>,
sqlstate: &str,
message: &str,
) {
let frame = BackendMessage::ErrorResponse(NoticeFields {
fields: vec![
(b'S', "FATAL".into()),
(b'C', sqlstate.into()),
(b'M', message.into()),
],
});
if let Err(e) = client.send(frame).await {
warn!(%e, sqlstate, "failed to send FATAL error response to client");
}
}
pub async fn send_error(
client: &mut Framed<TcpStream, FrontendCodec>,
sqlstate: &str,
message: &str,
status: TransactionStatus,
) -> anyhow::Result<()> {
client
.feed(BackendMessage::ErrorResponse(NoticeFields {
fields: vec![
(b'S', "ERROR".into()),
(b'C', sqlstate.into()),
(b'M', message.into()),
],
}))
.await?;
client.send(BackendMessage::ReadyForQuery(status)).await?;
Ok(())
}