use std::path::PathBuf;
use crate::exit;
use crate::key::KeyFileError;
use crate::private_file::PrivateFileError;
use crate::token::TokenFileError;
use crate::url::ParseUrlError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CliError {
#[error("{0}")]
Usage(String),
#[error("invalid --url")]
Url(#[source] ParseUrlError),
#[error("this command needs the operator key: pass --key-file or set STRATUM_KEY_FILE")]
MissingKeyFile,
#[error("key file {}", path.display())]
KeyFile {
path: PathBuf,
#[source]
source: KeyFileError,
},
#[error("token file {}", path.display())]
TokenFile {
path: PathBuf,
#[source]
source: TokenFileError,
},
#[error("file {}", path.display())]
File {
path: PathBuf,
#[source]
source: PrivateFileError,
},
#[error("file {}: {message}", path.display())]
FileContents {
path: PathBuf,
message: String,
},
#[error("cannot reach the node at {url}: {message}")]
Unreachable {
url: String,
message: String,
},
#[error("unexpected response from the node: {0}")]
Protocol(String),
#[error("{code}: {message}")]
Server {
status: u16,
code: String,
message: String,
details: Vec<String>,
},
#[error("refused: {0}")]
Refused(String),
#[error("the previewed configuration is invalid")]
InvalidPreview {
details: Vec<String>,
},
#[error("not applied: confirm interactively or pass --yes")]
NotConfirmed,
#[error("{0}")]
Local(String),
}
impl CliError {
pub fn exit_code(&self) -> u8 {
match self {
Self::Usage(_) | Self::Url(_) | Self::MissingKeyFile => exit::USAGE,
Self::KeyFile { .. }
| Self::TokenFile { .. }
| Self::File { .. }
| Self::FileContents { .. }
| Self::Local(_) => exit::FAILURE,
Self::Unreachable { .. } => exit::UNREACHABLE,
Self::Protocol(_) => exit::PROTOCOL,
Self::Server { code, .. } => exit::for_server_code(code),
Self::Refused(_) => exit::REFUSED,
Self::InvalidPreview { .. } => exit::INVALID,
Self::NotConfirmed => exit::NOT_CONFIRMED,
}
}
pub fn code(&self) -> &str {
match self {
Self::Usage(_) | Self::Url(_) | Self::MissingKeyFile => "usage",
Self::KeyFile { .. }
| Self::TokenFile { .. }
| Self::File { .. }
| Self::FileContents { .. }
| Self::Local(_) => "local",
Self::Unreachable { .. } => "unreachable",
Self::Protocol(_) => "protocol",
Self::Server { code, .. } => code,
Self::Refused(_) => "refused",
Self::InvalidPreview { .. } => "invalid",
Self::NotConfirmed => "not_confirmed",
}
}
pub fn details(&self) -> &[String] {
match self {
Self::Server { details, .. } | Self::InvalidPreview { details } => details,
_ => &[],
}
}
}