#[non_exhaustive]pub enum WireError {
Truncated,
BadEnvelope,
BadOffset,
NegativeOffset(i64),
BadPayload(ProtocolError),
}Expand description
Wire-layer error. Only WireError::Truncated is recoverable by
the caller (read more bytes and retry); the other variants signal
a corrupt or protocol-violating peer and call for dropping the
connection.
use kevy_replicate::wire::{WireError, decode_frame, encode_frame};
let set = kevy_resp::Argv::from(vec![b"SET".to_vec(), b"k".to_vec(), b"v".to_vec()]);
let bytes = encode_frame(0, &set);
// a partial read: keep the bytes and read more
assert_eq!(decode_frame(&bytes[..10]).unwrap_err(), WireError::Truncated);
// garbage: drop the peer
let err = decode_frame(b"+OK\r\n").unwrap_err();
assert_eq!(err, WireError::BadEnvelope);
assert_eq!(err.to_string(), "wire envelope not *2");Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
Truncated
Buffer ended before a complete frame; accumulate more bytes
and call decode_frame again.
use kevy_replicate::wire::{WireError, decode_frame, encode_frame};
let set = kevy_resp::Argv::from(vec![b"SET".to_vec(), b"k".to_vec(), b"v".to_vec()]);
let bytes = encode_frame(3, &set);
let (mut buf, mut decoded) = (Vec::new(), None);
for chunk in bytes.chunks(8) {
buf.extend_from_slice(chunk); // bytes arrive a few at a time
match decode_frame(&buf) {
Err(WireError::Truncated) => continue,
Ok((frame, used)) => decoded = Some((frame.offset, used)),
Err(e) => panic!("{e}"),
}
}
assert_eq!(decoded, Some((3, bytes.len())));BadEnvelope
Outer envelope did not start with *2\r\n (the only legal
envelope length).
use kevy_replicate::wire::{WireError, decode_frame};
// a three-element envelope is not a frame
let err = decode_frame(b"*3\r\n:1\r\n*0\r\n:0\r\n").unwrap_err();
assert_eq!(err, WireError::BadEnvelope);BadOffset
Offset element did not parse as a RESP integer (:N\r\n).
use kevy_replicate::wire::{WireError, decode_frame};
// the offset arrives as a bulk string instead of an integer
let err = decode_frame(b"*2\r\n$2\r\n42\r\n*1\r\n$4\r\nPING\r\n").unwrap_err();
assert_eq!(err, WireError::BadOffset);NegativeOffset(i64)
RESP integer parsed but is negative. Offsets are u64.
use kevy_replicate::wire::{WireError, decode_frame};
let err = decode_frame(b"*2\r\n:-7\r\n*1\r\n$4\r\nPING\r\n").unwrap_err();
assert_eq!(err, WireError::NegativeOffset(-7));BadPayload(ProtocolError)
Inner multi-bulk argv was malformed at the RESP layer.
use std::error::Error;
use kevy_replicate::wire::{WireError, decode_frame};
let err = decode_frame(b"*2\r\n:1\r\n*1\r\n!nope\r\n").unwrap_err();
assert!(matches!(err, WireError::BadPayload(_)));
assert!(err.source().is_some()); // the RESP parser's own errorTrait Implementations§
impl Copy for WireError
impl Eq for WireError
Source§impl Error for WireError
impl Error for WireError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl From<WireError> for ReplicaError
impl From<WireError> for ReplicaError
impl StructuralPartialEq for WireError
Auto Trait Implementations§
impl Freeze for WireError
impl RefUnwindSafe for WireError
impl Send for WireError
impl Sync for WireError
impl Unpin for WireError
impl UnsafeUnpin for WireError
impl UnwindSafe for WireError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more