use crate::{coding, message};
#[derive(thiserror::Error, Debug, Clone)]
pub enum Error {
#[error("webtransport error: {0}")]
WebTransport(#[from] web_transport::Error),
#[error("decode error: {0}")]
Decode(#[from] coding::DecodeError),
#[error("unsupported versions: client={0:?} server={1:?}")]
Version(message::Versions, message::Versions),
#[error("extension required: {0}")]
RequiredExtension(u64),
#[error("unexpected stream: {0:?}")]
UnexpectedStream(message::Stream),
#[error("varint bounds exceeded")]
BoundsExceeded(#[from] coding::BoundsExceeded),
#[error("duplicate")]
Duplicate,
#[error("cancelled")]
Cancel,
#[error("app code={0}")]
App(u32),
#[error("not found")]
NotFound,
#[error("wrong frame size")]
WrongSize,
#[error("protocol violation")]
ProtocolViolation,
}
impl Error {
pub fn to_code(&self) -> u32 {
match self {
Self::Cancel => 0,
Self::RequiredExtension(_) => 1,
Self::WebTransport(_) => 4,
Self::Decode(_) => 5,
Self::Version(..) => 9,
Self::UnexpectedStream(_) => 10,
Self::BoundsExceeded(_) => 11,
Self::Duplicate => 12,
Self::NotFound => 13,
Self::WrongSize => 14,
Self::ProtocolViolation => 15,
Self::App(app) => *app + 64,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;