eventsource_client/
error.rs1use crate::response::{ErrorBody, Response};
2
3#[derive(Debug)]
5pub struct HeaderError {
6 inner_error: Box<dyn std::error::Error + Send + Sync + 'static>,
8}
9
10impl HeaderError {
11 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#[derive(Debug)]
31pub enum Error {
32 TimedOut,
33 InvalidParameter(Box<dyn std::error::Error + Send + Sync + 'static>),
35 UnexpectedResponse(Response, ErrorBody),
37 HttpStream(Box<dyn std::error::Error + Send + Sync + 'static>),
39 Eof,
41 UnexpectedEof,
44 InvalidLine(String),
46 InvalidEvent,
47 MalformedLocationHeader(Box<dyn std::error::Error + Send + Sync + 'static>),
49 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>;