refactor(net): move NetError into its own error module

This commit is contained in:
Serkyo 2026-07-13 19:56:19 +02:00
parent aa9dc18efe
commit c6d8d1bd70
4 changed files with 48 additions and 40 deletions

View file

@ -4,45 +4,7 @@
//!
//! Encodes and decodes one logical protocol message per record on a QUIC stream, using a length prefix so a reader can recover record boundaries from a byte stream.
/// Errors produced by the framing codec and its stream I/O helpers.
#[derive(Debug, thiserror::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),
}
use crate::error::NetError;
/// The maximum payload length, in bytes, accepted on the control stream (64 KiB), matching the mod-payload cap. Higher-bandwidth tiers such as chunk streaming define their own caps.
pub const MAX_CONTROL_FRAME_LEN: usize = 64 * 1024;

View file

@ -15,7 +15,7 @@ use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, Server
use rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer, ServerName, UnixTime};
use tracing::debug;
use crate::codec::NetError;
use crate::error::NetError;
/// The Application-Layer Protocol Negotiation identifier for the Synvael protocol.
pub const ALPN: &[u8] = b"synvael";

45
crates/net/src/error.rs Normal file
View file

@ -0,0 +1,45 @@
// 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),
}

View file

@ -8,5 +8,6 @@
pub mod codec;
pub mod endpoint;
pub mod error;
pub mod handshake;
pub mod runtime;