Synvael/crates/net/src/error.rs

74 lines
3.4 KiB
Rust

// SPDX-License-Identifier: AGPL-3.0-only
//! Error types for the net crate.
use thiserror::Error;
/// Errors produced by the framing codec, endpoint construction, and stream I/O helpers.
#[derive(Debug, Error)]
pub enum NetError {
/// A `postcard` serialization or deserialization operation failed.
#[error("postcard codec error: {0}")]
Postcard(#[from] postcard::Error),
/// An underlying byte-stream I/O operation failed.
#[error("i/o error: {0}")]
Io(#[from] std::io::Error),
/// A declared frame length exceeded the caller-supplied maximum, indicating a malicious or corrupt peer.
#[error("frame length {len} exceeds maximum {max}")]
FrameTooLarge {
/// The frame length declared by the length prefix, in bytes.
len: u64,
/// The maximum payload length accepted by the reader, in bytes.
max: usize,
},
/// The stream ended before a complete frame (prefix or payload) had been read.
#[error("unexpected end of stream while reading a frame")]
UnexpectedEof,
/// A varint length prefix was malformed: either overlong or otherwise invalid.
#[error("malformed varint length prefix")]
MalformedVarint,
/// Writing bytes to a quinn send stream failed.
#[error("quinn write error: {0}")]
Write(#[from] quinn::WriteError),
/// Reading an exact number of bytes from a quinn recv stream failed.
#[error("quinn read error: {0}")]
Read(#[from] quinn::ReadExactError),
/// Generation of the self-signed server certificate failed.
#[error("certificate generation error: {0}")]
Rcgen(#[from] rcgen::Error),
/// Construction of the `rustls` TLS configuration failed.
#[error("rustls configuration error: {0}")]
Rustls(#[from] rustls::Error),
/// The `rustls` configuration lacked a TLS 1.3 cipher suite, which QUIC requires.
#[error("no initial cipher suite for quic: {0}")]
NoInitialCipherSuite(#[from] quinn::crypto::rustls::NoInitialCipherSuite),
}
/// Errors produced while performing the Synvael application handshake.
#[derive(Debug, Error)]
pub enum HandshakeError {
/// The synchronous `quinn` connect call failed before the connection attempt began.
#[error("quic connect error: {0}")]
Connect(#[from] quinn::ConnectError),
/// The QUIC connection failed to establish or was lost during the handshake.
#[error("quic connection error: {0}")]
Connection(#[from] quinn::ConnectionError),
/// A control-stream frame failed to encode, decode, or transfer.
#[error("control frame codec error: {0}")]
Codec(#[from] NetError),
/// The server refused the handshake. Carries the structured reason received (client side) or sent (server side) over the wire.
#[error("handshake rejected: {0:?}")]
Rejected(shared::protocol::HandshakeReject),
/// A control message other than the one expected for this handshake step arrived.
#[error("unexpected control message during handshake")]
UnexpectedMessage,
/// The client's protocol version did not match the server's. Returned locally by the server after it has sent a [`shared::protocol::HandshakeReject`] to the client.
#[error("protocol version mismatch: client {client}, server {server}")]
VersionMismatch {
/// Protocol version advertised by the client in its `ClientHello`.
client: u32,
/// Protocol version the server was built against (`shared::protocol::PROTOCOL_VERSION`).
server: u32,
},
}