use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::io::Error as IoError;
use std::str::from_utf8;
use http::Error as HttpError;
use http::StatusCode as HttpStatusCode;
use hyper::Error as HyperError;
use serde_json::Error as JsonError;
use thiserror::Error;
use url::ParseError;
use websocket_util::tungstenite::Error as WebSocketError;
use crate::Str;
#[derive(Debug, Error)]
pub enum RequestError<E> {
#[error("the endpoint reported an error")]
Endpoint(#[source] E),
#[error("the hyper crate reported an error")]
Hyper(
#[from]
#[source]
HyperError,
),
#[error("failed to read data")]
Io(
#[from]
#[source]
IoError,
),
}
#[derive(Clone, Debug, Error)]
pub struct HttpBody(Vec<u8>);
impl Display for HttpBody {
fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
match from_utf8(&self.0) {
Ok(s) => fmt.write_str(s)?,
Err(b) => write!(fmt, "{b:?}")?,
}
Ok(())
}
}
#[derive(Debug, Error)]
pub enum Error {
#[error("encountered an HTTP related error")]
Http(
#[from]
#[source]
HttpError,
),
#[error("encountered an unexpected HTTP status: {0}")]
HttpStatus(HttpStatusCode, #[source] HttpBody),
#[error("a JSON conversion failed")]
Json(
#[from]
#[source]
JsonError,
),
#[error("{0}")]
Str(Str),
#[error("failed to parse the URL")]
Url(
#[from]
#[source]
ParseError,
),
#[error("encountered a websocket related error")]
WebSocket(
#[from]
#[source]
WebSocketError,
),
}