atlas_http/
error.rs

1//use std::error::Error;
2use super::HttpRequest;
3use std::fmt;
4
5#[derive(Debug)]
6pub enum Error {
7    InvalidResponse(InvalidResponseError),
8    InvalidUri(String),
9    ProtoNotSupported(String),
10    NoConnect(String),
11    NoRead(InvalidResponseError),
12    NoWrite(String),
13    InvalidFirstLine(InvalidFirstLineError),
14    Io(std::io::Error),
15    FileNotExists(String),
16    FileNotCreated(FileNotCreatedError),
17    Custom(String),
18}
19
20#[derive(Debug)]
21pub struct InvalidResponseError {
22    pub url: String,
23    pub response: String,
24}
25
26#[derive(Debug)]
27pub struct InvalidFirstLineError {
28    pub request: HttpRequest,
29    pub first_line: String,
30}
31#[derive(Debug)]
32pub struct FileNotCreatedError {
33    pub filename: String,
34    pub error: String,
35}
36
37impl std::error::Error for Error {}
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Error::InvalidResponse(err) => write!(f, "InvalidResponse: Did not receive valid HTTP response from {}", err.url),
42            Error::InvalidUri(url) => write!(f, "InvalidUri: The supplied URL is invalid, {}", url),
43            Error::ProtoNotSupported(proto) => write!(f, "The '{}://' protocol is not supported.  Only the https:// and http:// protocols are supported.", proto),
44            Error::NoConnect(host) => write!(f, "Unable to connect to server at {}", host),
45            Error::NoRead(err) => write!(f, "Unable to read from server at URL {}, server error: {}", err.url, err.response),
46            Error::NoWrite(err) => write!(f, "Unable to write to server, server error: {}", err),
47            Error::InvalidFirstLine(err) => write!(f, "Received malformed first line within response: {}", err.first_line),
48            Error::Io(err) => write!(f, "HTTP IO: {}", err),
49            Error::FileNotExists(file_path) => write!(f, "Unable to upload file, as file does not exist at {}", file_path),
50        Error::FileNotCreated(err) => write!(f, "Unable to create file at {}, error: {}", err.filename, err.error),
51            Error::Custom(err) => write!(f, "HTTP Error: {}", err)
52        }
53    }
54}