Skip to content

Commit

Permalink
feat: make Error implement std::fmt::Display, std::error::Error` and …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
hkratz authored Mar 13, 2023
1 parent fcd962d commit 0eaab6e
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions eventsource-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ pub enum Error {
TimedOut,
StreamClosed,
/// An invalid request parameter
InvalidParameter(Box<dyn std::error::Error + Send + 'static>),
InvalidParameter(Box<dyn std::error::Error + Send + Sync + 'static>),
/// The HTTP response could not be handled.
UnexpectedResponse(StatusCode),
/// An error reading from the HTTP response body.
HttpStream(Box<dyn std::error::Error + Send + 'static>),
HttpStream(Box<dyn std::error::Error + Send + Sync + 'static>),
/// The HTTP response stream ended
Eof,
/// The HTTP response stream ended unexpectedly (e.g. in the
Expand All @@ -20,13 +20,32 @@ pub enum Error {
InvalidLine(String),
InvalidEvent,
/// Encountered a malformed Location header.
MalformedLocationHeader(Box<dyn std::error::Error + Send + 'static>),
MalformedLocationHeader(Box<dyn std::error::Error + Send + Sync + 'static>),
/// Reached maximum redirect limit after encountering Location headers.
MaxRedirectLimitReached(u32),
/// An unexpected failure occurred.
Unexpected(Box<dyn std::error::Error + Send + 'static>),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Error::*;
match self {
TimedOut => write!(f, "timed out"),
StreamClosed => write!(f, "stream closed"),
InvalidParameter(err) => write!(f, "invalid parameter: {err}"),
UnexpectedResponse(status_code) => write!(f, "unexpected response: {status_code}"),
HttpStream(err) => write!(f, "http error: {err}"),
Eof => write!(f, "eof"),
UnexpectedEof => write!(f, "unexpected eof"),
InvalidLine(line) => write!(f, "invalid line: {line}"),
InvalidEvent => write!(f, "invalid event"),
MalformedLocationHeader(err) => write!(f, "malformed header: {err}"),
MaxRedirectLimitReached(limit) => write!(f, "maximum redirect limit reached: {limit}"),
}
}
}

impl std::error::Error for Error {}

impl PartialEq<Error> for Error {
fn eq(&self, other: &Error) -> bool {
use Error::*;
Expand All @@ -50,19 +69,9 @@ impl Error {
pub fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::HttpStream(err) => Some(err.as_ref()),
Error::Unexpected(err) => Some(err.as_ref()),
_ => None,
}
}
}

impl<E> From<E> for Error
where
E: std::error::Error + Send + 'static,
{
fn from(e: E) -> Error {
Error::Unexpected(Box::new(e))
}
}

pub type Result<T> = std::result::Result<T, Error>;

0 comments on commit 0eaab6e

Please sign in to comment.