Expand description
§Error
This file is part of the Network Protocol project.
It defines the error types used throughout the protocol layer.
This module provides a unified error handling mechanism for the network protocol, encapsulating various error scenarios such as I/O errors, serialization issues, and protocol-specific logic failures.
It uses the thiserror crate for ergonomic error definition.
A custom Result<T> alias is provided to simplify signatures across the protocol stack.
The ProtocolError enum includes variants for:
- Invalid headers
- Unsupported protocol versions
- Oversized packets
- Encryption/decryption failures
- I/O and serialization errors
§Example Usage
use network_protocol::error::{ProtocolError, Result};
use std::fs::File;
use std::io::Read;
use tracing::{info, error};
fn read_file(path: &str) -> Result<String> {
let mut file = File::open(path).map_err(ProtocolError::Io)?;
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(ProtocolError::Io)?;
Ok(contents)
}
fn main() {
match read_file("example.txt") {
Ok(contents) => info!(contents, "Successfully read file"),
Err(e) => error!(error=%e, "Error reading file"),
}
}