1use thiserror::Error;
4use crate::message::{ErrorCode, ErrorDetails};
5
6#[derive(Debug, Error)]
8pub enum ProtocolError {
9 #[error("Serialization error: {0}")]
11 Serialization(String),
12
13 #[error("Invalid frame format")]
15 InvalidFrame,
16
17 #[error("Frame too large: {size} bytes (max: {max})")]
19 FrameTooLarge {
20 size: usize,
22 max: usize
24 },
25
26 #[error("Stream closed")]
28 StreamClosed,
29
30 #[error("Invalid stream ID: {0}")]
32 InvalidStreamId(u32),
33
34 #[error("Flow control violation")]
36 FlowControlViolation,
37}
38
39impl From<ErrorDetails> for ProtocolError {
40 fn from(details: ErrorDetails) -> Self {
41 match details.code {
42 ErrorCode::InvalidRequest => Self::InvalidFrame,
43 _ => Self::Serialization(details.message),
44 }
45 }
46}
47
48impl From<ProtocolError> for ErrorDetails {
49 fn from(error: ProtocolError) -> Self {
50 match error {
51 ProtocolError::Serialization(msg) => {
52 ErrorDetails::new(ErrorCode::InvalidRequest, msg)
53 }
54 ProtocolError::InvalidFrame => {
55 ErrorDetails::new(ErrorCode::InvalidRequest, "Invalid frame format")
56 }
57 ProtocolError::FrameTooLarge { size, max } => {
58 ErrorDetails::new(
59 ErrorCode::ResourceExhausted,
60 format!("Frame too large: {} bytes (max: {})", size, max)
61 )
62 }
63 ProtocolError::StreamClosed => {
64 ErrorDetails::new(ErrorCode::InternalError, "Stream closed")
65 }
66 ProtocolError::InvalidStreamId(id) => {
67 ErrorDetails::new(ErrorCode::InvalidRequest, format!("Invalid stream ID: {}", id))
68 }
69 ProtocolError::FlowControlViolation => {
70 ErrorDetails::new(ErrorCode::InternalError, "Flow control violation")
71 }
72 }
73 }
74}