hypothesis/
errors.rs

1//! API and CLI specific errors
2use std::fmt;
3
4use reqwest::header::InvalidHeaderValue;
5use serde::{Deserialize, Serialize};
6use thiserror::Error;
7
8#[derive(Error, Debug)]
9pub enum HypothesisError {
10    #[error("Make sure input fields are valid")]
11    APIError {
12        #[source]
13        source: APIError,
14        serde_error: Option<serde_json::Error>,
15        raw_text: String,
16    },
17    #[error("Invalid header value")]
18    HeaderError(#[from] InvalidHeaderValue),
19    #[error("Reqwest error")]
20    ReqwestError(#[from] reqwest::Error),
21    #[error("{suggestion:?}")]
22    EnvironmentError {
23        #[source]
24        source: std::env::VarError,
25        suggestion: String,
26    },
27    #[error("JSON format error")]
28    SerdeError(#[from] serde_json::Error),
29    #[error("Couldn't parse URL")]
30    URLError(#[from] url::ParseError),
31    #[error("Builder error: {0}")]
32    BuilderError(String),
33}
34
35/// Errors returned from the Hypothesis API
36#[derive(Error, Serialize, Deserialize, Debug, Default, Clone)]
37pub struct APIError {
38    /// API returned status
39    pub status: String,
40    /// Cause of failure
41    pub reason: String,
42}
43
44impl fmt::Display for APIError {
45    #[inline]
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(f, "Status: {}\nReason: {}", self.status, self.reason)
48    }
49}
50
51#[cfg(feature = "cli")]
52/// Errors returned from the Hypothesis CLI
53#[derive(Error, Serialize, Deserialize, Debug, Clone)]
54pub enum CLIError {
55    /// Thrown when Hypothesis client creation fails
56    #[error("Could not authorize")]
57    AuthorizationError,
58    /// Failed to parse a command line argument into its corresponding type
59    #[error("ParseError: {name:?} must be one of {types:?}")]
60    ParseError { name: String, types: Vec<String> },
61}