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

Error improvement #1

Draft
wants to merge 2 commits into
base: refactor/tokio-hyper
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ hyper-rustls = { version = "0.22.1", optional = true }
log = "0.4.6"
pin-project = "1.0.5"
tokio = { version = "1.2.0", features = ["time"] }
thiserror = "1.0.23"

[dev-dependencies]
env_logger = "0.7.1"
Expand Down
2 changes: 1 addition & 1 deletion examples/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() -> Result<(), es::Error> {
Ok(())
}

fn tail_events(mut client: es::Client<es::HttpsConnector>) -> impl Stream<Item = Result<(), ()>> {
fn tail_events(client: es::Client<es::HttpsConnector>) -> impl Stream<Item = Result<(), ()>> {
client
.stream()
.map_ok(|event| {
Expand Down
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<C> Client<C> {
///
/// After the first successful connection, the stream will
/// reconnect for retryable errors.
pub fn stream(&mut self) -> EventStream<C>
pub fn stream(&self) -> EventStream<C>
where
C: Connect + Clone + Send + Sync + 'static,
{
Expand Down
24 changes: 9 additions & 15 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
/// Error type returned from this library's functions.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// The HTTP request failed.
#[error("HTTP request failed: {0}")]
HttpRequest(Box<dyn std::error::Error + Send + 'static>),
/// An error reading from the HTTP response body.
#[error("error reading from the HTTP response body: {0}")]
HttpStream(Box<dyn std::error::Error + Send + 'static>),
/// The HTTP response stream ended unexpectedly (e.g. in the
/// middle of an event).
/// e.g. in the middle of an event.
#[error("HTTP response stream ended unexpectedly")]
UnexpectedEof,
/// Encountered a line not conforming to the SSE protocol.
#[error("Invalid line encountered")]
InvalidLine(String),
/// Encountered an event type that is not a valid UTF-8 byte sequence.
#[error("Event type is not valid UTF-8")]
InvalidEventType(std::str::Utf8Error),
/// An unexpected failure occurred.
Unexpected(Box<dyn std::error::Error + Send + 'static>),
#[error("Unexpected failure: {0}")]
Unexpected(#[from] Box<dyn std::error::Error + Send + 'static>),
}

impl PartialEq<Error> for Error {
Expand Down Expand Up @@ -46,13 +49,4 @@ impl Error {
}
}

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>;