use std::fmt;
use std::io;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, ProfileError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ByokErrorCode {
VaultUnavailable,
VaultLocked,
VaultDenied,
MasterKeyMissing,
CasBindingKeyMissing,
MalformedKeyStore,
InvalidCredential,
EncryptionFailed,
}
impl ByokErrorCode {
pub const fn as_str(self) -> &'static str {
match self {
Self::VaultUnavailable => "vault-unavailable",
Self::VaultLocked => "vault-locked",
Self::VaultDenied => "vault-denied",
Self::MasterKeyMissing => "master-key-missing",
Self::CasBindingKeyMissing => "cas-binding-key-missing",
Self::MalformedKeyStore => "malformed-key-store",
Self::InvalidCredential => "invalid-credential",
Self::EncryptionFailed => "encryption-failed",
}
}
}
impl fmt::Display for ByokErrorCode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Debug, Error)]
pub enum ProfileError {
#[error("Profile not found: {0}")]
ProfileNotFound(String),
#[error("Invalid API key provider: {0}")]
InvalidProvider(String),
#[error("{0}")]
MissingCredentials(String),
#[error("{0}")]
Auth(String),
#[error("{code}: {message}")]
Byok {
code: ByokErrorCode,
message: String,
},
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("{0}")]
Storage(#[from] squigit_storage::StorageError),
#[error("Network error: {0}")]
Network(#[from] reqwest::Error),
#[error("URL error: {0}")]
Url(#[from] url::ParseError),
}
impl ProfileError {
pub fn byok(code: ByokErrorCode, message: impl Into<String>) -> Self {
Self::Byok {
code,
message: message.into(),
}
}
}