twitter_stream/
error.rs

1//! Error type
2
3pub use http::StatusCode;
4
5use std::error;
6use std::fmt::{self, Display, Formatter};
7use std::str::Utf8Error;
8
9/// An error occurred while trying to connect to a Stream.
10#[derive(Debug)]
11pub enum Error<E = Box<dyn error::Error + Send + Sync>> {
12    /// An HTTP error from the Stream.
13    Http(StatusCode),
14    /// Error from the underlying HTTP client while receiving an HTTP response or reading the body.
15    Service(E),
16    /// Twitter returned a non-UTF-8 string.
17    Utf8(Utf8Error),
18}
19
20impl<E: error::Error + 'static> error::Error for Error<E> {
21    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
22        use crate::Error::*;
23
24        match *self {
25            Http(_) => None,
26            Service(ref e) => Some(e),
27            Utf8(ref e) => Some(e),
28        }
29    }
30}
31
32impl<E: Display> Display for Error<E> {
33    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
34        use crate::Error::*;
35
36        match *self {
37            Http(ref code) => write!(f, "HTTP status code: {}", code),
38            Service(ref e) => write!(f, "HTTP client error: {}", e),
39            Utf8(ref e) => Display::fmt(e, f),
40        }
41    }
42}