use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use entropy_kvdb::kv_manager::error::KvError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum BackupProviderError {
#[error("HTTP request: {0}")]
HttpRequest(#[from] reqwest::Error),
#[error("Key-value store: {0}")]
Kv(#[from] KvError),
#[error("Encryption key is not present in backup store")]
NoKeyInStore,
#[error("Cannot retrieve associated nonce for this backup")]
NoNonceInStore,
#[error("Panic while holding lock on backup store")]
RwLockPoison,
#[error("JSON: {0}")]
SerdeJson(#[from] serde_json::Error),
#[error("Encryption: {0}")]
Encryption(#[from] crate::validation::EncryptedSignedMessageErr),
#[error("Attestation: {0}")]
Attestation(#[from] crate::attestation::errors::AttestationErr),
#[error("Generic Substrate error: {0}")]
GenericSubstrate(#[from] subxt::error::Error),
#[error("Backup provider was unreachable or failed to make backup: {0}")]
FailedToMakeBackup(String),
#[error("Provider responded with a key which is not 32 bytes")]
BadKeyLength,
#[error("Substrate: {0}")]
SubstrateClient(#[from] entropy_client::substrate::SubstrateError),
#[error("The account requesting to recover a key is not registered with the staking pallet")]
NotRegisteredWithStakingPallet,
#[error("Filesystem IO: {0}")]
Io(#[from] std::io::Error),
#[error("Utf8Error: {0:?}")]
Utf8(#[from] std::str::Utf8Error),
#[error("Quote parse: {0}")]
QuoteParse(#[from] tdx_quote::QuoteParseError),
#[error("Bad quote input data: TSS account, response public key, or nonce are incorrect")]
BadQuoteInputData,
#[error("Quote verify: {0}")]
VerifyQuote(#[from] entropy_shared::attestation::VerifyQuoteError),
#[error("Could not find another TSS node to request backup")]
NoValidators,
#[error("Could not get server info for TSS node chosen for backup")]
NoServerInfo,
#[error("Node has started fresh and not yet successfully set up")]
NotReady,
#[error("Quote measurement: {0}")]
QuoteMeasurement(#[from] crate::attestation::errors::QuoteMeasurementErr),
#[error("Timed out waiting to be connected to chain")]
NotConnectedToChain,
#[error("Application State Error: {0}")]
AppStateError(#[from] crate::helpers::app_state::AppStateError),
#[error("Failed to retrieve nonce from backup provider during recovery: {0}")]
FailedToRetrieveNonce(String),
#[error("Failed to retrieve encryption key from backup provider during recovery: {0}")]
FailedToRetrieveKey(String),
}
impl IntoResponse for BackupProviderError {
fn into_response(self) -> Response {
tracing::error!("{:?}", format!("{self}"));
let body = format!("{self}").into_bytes();
(StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
}
}