Skip to content

Commit 0eaab6e

Browse files
authored
feat: make Error implement std::fmt::Display, std::error::Error` and Sync (#47)
This is necessary to make it work with error-wrapping libraries like [anyhow](https://crates.io/crates/anyhow) or [eyre](https://crates.io/crates/eyre). Also remove Error::Unexpected as it appeared unused.
1 parent fcd962d commit 0eaab6e

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

eventsource-client/src/error.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ pub enum Error {
66
TimedOut,
77
StreamClosed,
88
/// An invalid request parameter
9-
InvalidParameter(Box<dyn std::error::Error + Send + 'static>),
9+
InvalidParameter(Box<dyn std::error::Error + Send + Sync + 'static>),
1010
/// The HTTP response could not be handled.
1111
UnexpectedResponse(StatusCode),
1212
/// An error reading from the HTTP response body.
13-
HttpStream(Box<dyn std::error::Error + Send + 'static>),
13+
HttpStream(Box<dyn std::error::Error + Send + Sync + 'static>),
1414
/// The HTTP response stream ended
1515
Eof,
1616
/// The HTTP response stream ended unexpectedly (e.g. in the
@@ -20,13 +20,32 @@ pub enum Error {
2020
InvalidLine(String),
2121
InvalidEvent,
2222
/// Encountered a malformed Location header.
23-
MalformedLocationHeader(Box<dyn std::error::Error + Send + 'static>),
23+
MalformedLocationHeader(Box<dyn std::error::Error + Send + Sync + 'static>),
2424
/// Reached maximum redirect limit after encountering Location headers.
2525
MaxRedirectLimitReached(u32),
26-
/// An unexpected failure occurred.
27-
Unexpected(Box<dyn std::error::Error + Send + 'static>),
2826
}
2927

28+
impl std::fmt::Display for Error {
29+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30+
use Error::*;
31+
match self {
32+
TimedOut => write!(f, "timed out"),
33+
StreamClosed => write!(f, "stream closed"),
34+
InvalidParameter(err) => write!(f, "invalid parameter: {err}"),
35+
UnexpectedResponse(status_code) => write!(f, "unexpected response: {status_code}"),
36+
HttpStream(err) => write!(f, "http error: {err}"),
37+
Eof => write!(f, "eof"),
38+
UnexpectedEof => write!(f, "unexpected eof"),
39+
InvalidLine(line) => write!(f, "invalid line: {line}"),
40+
InvalidEvent => write!(f, "invalid event"),
41+
MalformedLocationHeader(err) => write!(f, "malformed header: {err}"),
42+
MaxRedirectLimitReached(limit) => write!(f, "maximum redirect limit reached: {limit}"),
43+
}
44+
}
45+
}
46+
47+
impl std::error::Error for Error {}
48+
3049
impl PartialEq<Error> for Error {
3150
fn eq(&self, other: &Error) -> bool {
3251
use Error::*;
@@ -50,19 +69,9 @@ impl Error {
5069
pub fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
5170
match self {
5271
Error::HttpStream(err) => Some(err.as_ref()),
53-
Error::Unexpected(err) => Some(err.as_ref()),
5472
_ => None,
5573
}
5674
}
5775
}
5876

59-
impl<E> From<E> for Error
60-
where
61-
E: std::error::Error + Send + 'static,
62-
{
63-
fn from(e: E) -> Error {
64-
Error::Unexpected(Box::new(e))
65-
}
66-
}
67-
6877
pub type Result<T> = std::result::Result<T, Error>;

0 commit comments

Comments
 (0)