hypothesis_rs/
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:\n{source}\n{raw_text}")]
11    APIError {
12        #[source]
13        source: APIError,
14        serde_error: Option<serde_json::Error>,
15        raw_text: String,
16    },
17    #[error("Invalid header value: {0}")]
18    HeaderError(#[from] InvalidHeaderValue),
19    #[error("Reqwest error: {0}")]
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: {0}")]
28    SerdeError(#[from] serde_json::Error),
29    #[error("Time format error: {0}")]
30    TimeError(#[from] time::error::Error),
31    #[error("Couldn't parse URL: {0}")]
32    URLError(#[from] url::ParseError),
33    #[error("Builder error: {0}")]
34    BuilderError(String),
35}
36
37/// Errors returned from the Hypothesis API
38#[derive(Error, Serialize, Deserialize, Debug, Default, Clone)]
39pub struct APIError {
40    /// API returned status
41    pub status: String,
42    /// Cause of failure
43    pub reason: String,
44}
45
46impl fmt::Display for APIError {
47    #[inline]
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        write!(f, "Status: {}\nReason: {}", self.status, self.reason)
50    }
51}
52
53#[cfg(feature = "cli")]
54/// Errors returned from the Hypothesis CLI
55#[derive(Error, Serialize, Deserialize, Debug, Clone)]
56pub enum CLIError {
57    /// Thrown when Hypothesis client creation fails
58    #[error("Could not authorize")]
59    AuthorizationError,
60    /// Failed to parse a command line argument into its corresponding type
61    #[error("ParseError: {name:?} must be one of {types:?}")]
62    ParseError { name: String, types: Vec<String> },
63}