Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Error implement std::fmt::Display, std::error::Error and Sync #47

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;