Skip to main content

qail_pg/protocol/
error.rs

1//! Encoding errors for PostgreSQL wire protocol.
2//!
3//! Shared by `PgEncoder` and `AstEncoder`.
4
5use std::fmt;
6
7use qail_core::ast::Action;
8
9/// Errors that can occur during wire protocol encoding.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum EncodeError {
12    /// A string value contains a literal NULL byte (0x00).
13    NullByte,
14    /// Too many parameters for the protocol (limit is i16::MAX = 32767).
15    TooManyParameters(usize),
16    /// A single parameter or message exceeds i32::MAX bytes.
17    MessageTooLarge(usize),
18    /// Action not supported by the AST-native encoder (e.g. Listen, Search).
19    UnsupportedAction(Action),
20    /// A Value::Function/expression contains SQL injection markers.
21    UnsafeExpression(String),
22}
23
24impl fmt::Display for EncodeError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            EncodeError::NullByte => {
28                write!(f, "Value contains NULL byte (0x00) which is invalid in PostgreSQL")
29            }
30            EncodeError::TooManyParameters(count) => {
31                write!(f, "Too many parameters: {} (Limit is 32767)", count)
32            }
33            EncodeError::MessageTooLarge(size) => {
34                write!(f, "Message too large: {} bytes (Limit is {})", size, i32::MAX)
35            }
36            EncodeError::UnsupportedAction(action) => {
37                write!(f, "Unsupported action {:?} in AST-native encoder", action)
38            }
39            EncodeError::UnsafeExpression(expr) => {
40                write!(f, "Unsafe expression rejected: {}", expr)
41            }
42        }
43    }
44}
45
46impl std::error::Error for EncodeError {}