Skip to main content

reqwest_sse/
error.rs

1use core::error::Error;
2use reqwest::{StatusCode, header::HeaderValue};
3use std::fmt::Display;
4
5#[derive(Debug)]
6pub enum EventError {
7    IoError(std::io::Error),
8}
9
10impl Error for EventError {}
11
12impl Display for EventError {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        match self {
15            EventError::IoError(error) => {
16                write!(f, "failed to process event due to I/O error: {error}")
17            }
18        }
19    }
20}
21
22#[derive(Debug, PartialEq, Eq)]
23pub enum EventSourceError {
24    BadStatus(StatusCode),
25    BadContentType(Option<HeaderValue>),
26}
27
28impl Error for EventSourceError {}
29
30impl Display for EventSourceError {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            EventSourceError::BadStatus(status_code) => {
34                write!(f, "expecting status code `200`, found: {status_code}")
35            }
36            EventSourceError::BadContentType(None) => {
37                write!(
38                    f,
39                    "expecting \"text/event-stream\" content type, found none"
40                )
41            }
42            EventSourceError::BadContentType(Some(header_value)) => {
43                let content_type = header_value.to_str();
44                match content_type {
45                    Ok(content_type) => {
46                        write!(
47                            f,
48                            "expecting \"text/event-stream\", found: \"{content_type}\"",
49                        )
50                    }
51                    Err(_) => {
52                        write!(f, "expecting \"text/event-stream\", found invalid value")
53                    }
54                }
55            }
56        }
57    }
58}