h3/error/
internal_error.rs1use std::error::Error;
4
5use crate::{frame::FrameProtocolError, quic::ConnectionErrorIncoming};
6
7use super::codes::Code;
8use std::fmt::Display;
9
10#[derive(Debug, Clone, Hash)]
17pub struct InternalConnectionError {
18 pub(crate) code: Code,
20 pub(crate) message: String,
22}
23
24impl InternalConnectionError {
25 pub fn new(code: Code, message: String) -> Self {
27 Self { code, message }
28 }
29 pub fn got_frame_error(value: FrameProtocolError) -> Self {
31 match value {
32 FrameProtocolError::InvalidStreamId(id) => InternalConnectionError {
33 code: Code::H3_ID_ERROR,
34 message: format!("invalid stream id: {}", id),
35 },
36 FrameProtocolError::InvalidPushId(id) => InternalConnectionError {
37 code: Code::H3_ID_ERROR,
38 message: format!("invalid push id: {}", id),
39 },
40 FrameProtocolError::Settings(error) => InternalConnectionError {
41 code: Code::H3_SETTINGS_ERROR,
45 message: error.to_string(),
46 },
47 FrameProtocolError::ForbiddenFrame(number) => InternalConnectionError {
52 code: Code::H3_FRAME_UNEXPECTED,
53 message: format!("received a forbidden frame with number {}", number),
54 },
55 FrameProtocolError::InvalidFrameValue | FrameProtocolError::Malformed => InternalConnectionError {
61 code: Code::H3_FRAME_ERROR,
62 message: "frame payload that contains additional bytes after the identified fields or a frame payload that terminates before the end of the identified fields".to_string(),
63 },
64 }
65 }
66}
67
68#[derive(Debug, Clone)]
70pub enum ErrorOrigin {
71 Internal(InternalConnectionError),
73 Quic(ConnectionErrorIncoming),
75}
76
77impl Display for ErrorOrigin {
78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79 match self {
80 ErrorOrigin::Internal(error) => write!(f, "Internal Error: {}", error.message),
81 ErrorOrigin::Quic(error) => write!(f, "Quic Error: {:?}", error),
82 }
83 }
84}
85
86impl Error for ErrorOrigin {}
87
88impl From<InternalConnectionError> for ErrorOrigin {
89 fn from(error: InternalConnectionError) -> Self {
90 ErrorOrigin::Internal(error)
91 }
92}
93
94impl From<ConnectionErrorIncoming> for ErrorOrigin {
95 fn from(error: ConnectionErrorIncoming) -> Self {
96 ErrorOrigin::Quic(error)
97 }
98}