1use std::path::PathBuf;
2
3pub type Result<T> = std::result::Result<T, InquisitorError>;
4
5#[derive(Debug)]
7pub enum InquisitorError {
8 DurationParseError,
9 SignalHandlerCouldNotBeSet(ctrlc::Error),
10 HistogramCouldNotBeCreated(hdrhistogram::CreationError),
11 InvalidRegex(regex::Error),
12 CouldNotOpenFile(std::io::Error, PathBuf),
13 CouldNotReadFile(std::io::Error, PathBuf),
14 CouldNotConvertCert(reqwest::Error, PathBuf),
15 FailedToCreateClient(reqwest::Error),
16 FailedToGetTimeInterval(std::time::SystemTimeError),
17 FailedToBuildAsyncRuntime(tokio::io::Error),
18 FailedToUnwrapArc,
19 FailedToReadResponseBody(reqwest::Error),
20 FailedToRecordToHistogram(hdrhistogram::RecordError),
21}
22
23impl std::fmt::Display for InquisitorError {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
25 match self {
26 Self::DurationParseError => write!(f, "Failed to parse duration"),
27 Self::SignalHandlerCouldNotBeSet(e) => write!(f, "Could not set signal handler: {e}"),
28 Self::HistogramCouldNotBeCreated(e) => {
29 write!(f, "Could not create histogram for response times: {e}")
30 }
31 Self::InvalidRegex(e) => write!(f, "Invalid regex: {e}"),
32 Self::CouldNotOpenFile(e, file) => {
33 write!(f, "Could not open file '{}': {e}", file.to_string_lossy())
34 }
35 Self::CouldNotReadFile(e, file) => {
36 write!(f, "Could not read file '{}': {e}", file.to_string_lossy())
37 }
38 Self::CouldNotConvertCert(e, file) => {
39 write!(
40 f,
41 "Could not convert file '{}' to PEM certificate: {e}",
42 file.to_string_lossy()
43 )
44 }
45 Self::FailedToCreateClient(e) => write!(f, "Failed to build HTTP client: {e}"),
46 Self::FailedToGetTimeInterval(e) => write!(f, "Failed to get elapsed time: {e}"),
47 Self::FailedToBuildAsyncRuntime(e) => write!(f, "Failed to crate async runtime: {e}"),
48 Self::FailedToUnwrapArc => write!(
49 f,
50 "Bug: Arc used to stored request times could not be unwrapped"
51 ),
52 Self::FailedToReadResponseBody(e) => write!(f, "Failed to read response body: {e}"),
53 Self::FailedToRecordToHistogram(e) => write!(f, "Failed to record to histogram: {e}"),
54 }
55 }
56}
57
58impl std::error::Error for InquisitorError {}
59
60impl From<ctrlc::Error> for InquisitorError {
61 fn from(e: ctrlc::Error) -> Self {
62 Self::SignalHandlerCouldNotBeSet(e)
63 }
64}
65
66impl From<hdrhistogram::CreationError> for InquisitorError {
67 fn from(e: hdrhistogram::CreationError) -> Self {
68 Self::HistogramCouldNotBeCreated(e)
69 }
70}
71
72impl From<regex::Error> for InquisitorError {
73 fn from(e: regex::Error) -> Self {
74 Self::InvalidRegex(e)
75 }
76}
77
78impl From<tokio::io::Error> for InquisitorError {
79 fn from(e: tokio::io::Error) -> Self {
80 Self::FailedToBuildAsyncRuntime(e)
81 }
82}
83
84impl From<std::time::SystemTimeError> for InquisitorError {
85 fn from(e: std::time::SystemTimeError) -> Self {
86 Self::FailedToGetTimeInterval(e)
87 }
88}
89
90impl From<hdrhistogram::RecordError> for InquisitorError {
91 fn from(e: hdrhistogram::RecordError) -> Self {
92 Self::FailedToRecordToHistogram(e)
93 }
94}