Skip to main content

krabka_protocol/
error.rs

1use thiserror::Error;
2
3/// Errors that can occur during wire-protocol encoding or decoding.
4#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum ProtocolError {
7    /// Decode reached end of buffer before the expected number of bytes.
8    #[error("unexpected end of buffer: needed {needed} more bytes")]
9    UnexpectedEof { needed: usize },
10
11    /// Decoded a value that the schema says is impossible, such as a negative
12    /// array length.
13    #[error("invalid value: {0}")]
14    InvalidValue(&'static str),
15
16    /// Decoded UTF-8 bytes that are not valid UTF-8.
17    #[error("invalid UTF-8 in string field")]
18    InvalidUtf8(#[source] std::str::Utf8Error),
19
20    /// Decoded a varint that exceeds the maximum legal length.
21    #[error("varint exceeds {max} bytes")]
22    VarintTooLong { max: usize },
23
24    /// Encountered an unknown API version for a known API key.
25    #[error("unsupported API version {version} for api key {api_key}")]
26    UnsupportedVersion { api_key: i16, version: i16 },
27
28    /// Schema version requested is not within the message's supported range.
29    #[error("schema mismatch: {0}")]
30    SchemaMismatch(&'static str),
31
32    /// Decoded a known KIP-482 tag at a version that its schema does not
33    /// declare. The text matches the exception that Kafka's generated
34    /// `read` method throws.
35    #[error("Tag {tag} is not valid for version {version}")]
36    TagNotValidForVersion { tag: u32, version: i16 },
37}
38
39#[cfg(test)]
40mod tests {
41
42    use super::*;
43
44    #[test]
45    fn display_is_useful() {
46        let e = ProtocolError::UnexpectedEof { needed: 4 };
47        assert2::assert!(e.to_string() == "unexpected end of buffer: needed 4 more bytes");
48    }
49}