Skip to main content

httpgenerator_cli/
error.rs

1use std::{error::Error, fmt, path::PathBuf};
2
3use httpgenerator_core::openapi::OpenApiSpecificationVersion;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum CliError {
7    MissingInput,
8    InspectOpenApi(String),
9    LoadOpenApi(String),
10    UnsupportedValidationVersion {
11        version: OpenApiSpecificationVersion,
12    },
13    CreateOutputDirectory {
14        path: PathBuf,
15        reason: String,
16    },
17    WriteFiles {
18        path: PathBuf,
19        reason: String,
20    },
21    WriteTimeout {
22        seconds: u64,
23    },
24    WriteChannelClosed,
25}
26
27impl fmt::Display for CliError {
28    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::MissingInput => write!(
31                formatter,
32                "missing OpenAPI input path or URL; run with --help for usage"
33            ),
34            Self::InspectOpenApi(reason) => write!(formatter, "{reason}"),
35            Self::LoadOpenApi(reason) => write!(formatter, "{reason}"),
36            Self::UnsupportedValidationVersion { version } => write!(
37                formatter,
38                "{version} documents are not supported by CLI validation yet; retry with --skip-validation"
39            ),
40            Self::CreateOutputDirectory { path, reason } => write!(
41                formatter,
42                "failed to create output directory '{}': {reason}",
43                path.display()
44            ),
45            Self::WriteFiles { path, reason } => write!(
46                formatter,
47                "failed to write generated file '{}': {reason}",
48                path.display()
49            ),
50            Self::WriteTimeout { seconds } => write!(
51                formatter,
52                "timed out after {seconds} second(s) while writing generated files"
53            ),
54            Self::WriteChannelClosed => write!(
55                formatter,
56                "file writing worker stopped before reporting a result"
57            ),
58        }
59    }
60}
61
62impl Error for CliError {}
63
64impl CliError {
65    pub const fn telemetry_name(&self) -> &'static str {
66        match self {
67            Self::MissingInput => "MissingInput",
68            Self::InspectOpenApi(_) => "InspectOpenApi",
69            Self::LoadOpenApi(_) => "LoadOpenApi",
70            Self::UnsupportedValidationVersion { .. } => "UnsupportedValidationVersion",
71            Self::CreateOutputDirectory { .. } => "CreateOutputDirectory",
72            Self::WriteFiles { .. } => "WriteFiles",
73            Self::WriteTimeout { .. } => "WriteTimeout",
74            Self::WriteChannelClosed => "WriteChannelClosed",
75        }
76    }
77}