1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! API and CLI specific errors
use std::fmt;

use reqwest::header::InvalidHeaderValue;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum HypothesisError {
    #[error("Make sure input fields are valid")]
    APIError(#[from] APIError),
    #[error("Invalid header value")]
    HeaderError(#[from] InvalidHeaderValue),
    #[error("Reqwest error")]
    ReqwestError(#[from] reqwest::Error),
    #[error("{suggestion:?}")]
    EnvironmentError {
        #[source]
        source: std::env::VarError,
        suggestion: String,
    },
    #[error("JSON format error")]
    SerdeError(#[from] serde_json::Error),
    #[error("Couldn't parse URL")]
    URLError(#[from] url::ParseError),
    #[error("Builder error: {0}")]
    BuilderError(String),
}

/// Errors returned from the Hypothesis API
#[derive(Error, Serialize, Deserialize, Debug, Default, Clone)]
pub struct APIError {
    /// API returned status
    pub status: String,
    /// Cause of failure
    pub reason: String,
}

impl fmt::Display for APIError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Status: {}\nReason: {}", self.status, self.reason)
    }
}

#[cfg(feature = "cli")]
/// Errors returned from the Hypothesis CLI
#[derive(Error, Serialize, Deserialize, Debug, Clone)]
pub enum CLIError {
    /// Thrown when Hypothesis client creation fails
    #[error("Could not authorize")]
    AuthorizationError,
    /// Failed to parse a command line argument into its corresponding type
    #[error("ParseError: {name:?} must be one of {types:?}")]
    ParseError { name: String, types: Vec<String> },
}