1use thiserror::Error;
2
3#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum ProtocolError {
7 #[error("unexpected end of buffer: needed {needed} more bytes")]
9 UnexpectedEof { needed: usize },
10
11 #[error("invalid value: {0}")]
14 InvalidValue(&'static str),
15
16 #[error("invalid UTF-8 in string field")]
18 InvalidUtf8(#[source] std::str::Utf8Error),
19
20 #[error("varint exceeds {max} bytes")]
22 VarintTooLong { max: usize },
23
24 #[error("unsupported API version {version} for api key {api_key}")]
26 UnsupportedVersion { api_key: i16, version: i16 },
27
28 #[error("schema mismatch: {0}")]
30 SchemaMismatch(&'static str),
31
32 #[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}