1use std::str::Utf8Error;
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
7#[non_exhaustive]
8pub enum ProtocolError {
9 #[error("unexpected end of buffer: needed {needed} more bytes")]
11 UnexpectedEof { needed: usize },
12
13 #[error("invalid value: {0}")]
16 InvalidValue(&'static str),
17
18 #[error("invalid UTF-8 in string field")]
20 InvalidUtf8(#[source] Utf8Error),
21
22 #[error("varint exceeds {max} bytes")]
24 VarintTooLong { max: usize },
25
26 #[error("unsupported API version {version} for api key {api_key}")]
28 UnsupportedVersion { api_key: i16, version: i16 },
29
30 #[error("schema mismatch: {0}")]
32 SchemaMismatch(&'static str),
33
34 #[error("Tag {tag} is not valid for version {version}")]
38 TagNotValidForVersion { tag: u32, version: i16 },
39
40 #[error("{what} length {len} does not fit its wire-format length prefix")]
43 LengthOverflow { what: &'static str, len: usize },
44}
45
46#[cfg(test)]
47mod tests {
48
49 use super::*;
50
51 #[test]
52 fn display_is_useful() {
53 let e = ProtocolError::UnexpectedEof { needed: 4 };
54 assert2::assert!(e.to_string() == "unexpected end of buffer: needed 4 more bytes");
55 }
56}