eventsource_client/
error.rs

1use crate::response::{ErrorBody, Response};
2
3/// Error type for invalid response headers encountered in ResponseDetails.
4#[derive(Debug)]
5pub struct HeaderError {
6    /// Wrapped inner error providing details about the header issue.
7    inner_error: Box<dyn std::error::Error + Send + Sync + 'static>,
8}
9
10impl HeaderError {
11    /// Constructs a new `HeaderError` wrapping an existing error.
12    pub fn new(err: Box<dyn std::error::Error + Send + Sync + 'static>) -> Self {
13        HeaderError { inner_error: err }
14    }
15}
16
17impl std::fmt::Display for HeaderError {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(f, "Invalid response header: {}", self.inner_error)
20    }
21}
22
23impl std::error::Error for HeaderError {
24    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
25        Some(self.inner_error.as_ref())
26    }
27}
28
29/// Error type returned from this library's functions.
30#[derive(Debug)]
31pub enum Error {
32    TimedOut,
33    /// An invalid request parameter
34    InvalidParameter(Box<dyn std::error::Error + Send + Sync + 'static>),
35    /// The HTTP response could not be handled.
36    UnexpectedResponse(Response, ErrorBody),
37    /// An error reading from the HTTP response body.
38    HttpStream(Box<dyn std::error::Error + Send + Sync + 'static>),
39    /// The HTTP response stream ended
40    Eof,
41    /// The HTTP response stream ended unexpectedly (e.g. in the
42    /// middle of an event).
43    UnexpectedEof,
44    /// Encountered a line not conforming to the SSE protocol.
45    InvalidLine(String),
46    InvalidEvent,
47    /// Encountered a malformed Location header.
48    MalformedLocationHeader(Box<dyn std::error::Error + Send + Sync + 'static>),
49    /// Reached maximum redirect limit after encountering Location headers.
50    MaxRedirectLimitReached(u32),
51}
52
53impl std::fmt::Display for Error {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        use Error::*;
56        match self {
57            TimedOut => write!(f, "timed out"),
58            InvalidParameter(err) => write!(f, "invalid parameter: {err}"),
59            UnexpectedResponse(r, _) => {
60                let status = r.status();
61                write!(f, "unexpected response: {status}")
62            }
63            HttpStream(err) => write!(f, "http error: {err}"),
64            Eof => write!(f, "eof"),
65            UnexpectedEof => write!(f, "unexpected eof"),
66            InvalidLine(line) => write!(f, "invalid line: {line}"),
67            InvalidEvent => write!(f, "invalid event"),
68            MalformedLocationHeader(err) => write!(f, "malformed header: {err}"),
69            MaxRedirectLimitReached(limit) => write!(f, "maximum redirect limit reached: {limit}"),
70        }
71    }
72}
73
74impl std::error::Error for Error {}
75
76impl PartialEq<Error> for Error {
77    fn eq(&self, other: &Error) -> bool {
78        use Error::*;
79        if let (InvalidLine(msg1), InvalidLine(msg2)) = (self, other) {
80            return msg1 == msg2;
81        } else if let (UnexpectedEof, UnexpectedEof) = (self, other) {
82            return true;
83        }
84        false
85    }
86}
87
88impl Error {
89    pub fn is_http_stream_error(&self) -> bool {
90        if let Error::HttpStream(_) = self {
91            return true;
92        }
93        false
94    }
95
96    pub fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
97        match self {
98            Error::HttpStream(err) => Some(err.as_ref()),
99            _ => None,
100        }
101    }
102}
103
104pub type Result<T> = std::result::Result<T, Error>;