use error_support::{ErrorHandling, GetErrorHandling};
pub type Result<T, E = PushError> = std::result::Result<T, E>;
pub type ApiResult<T, E = PushApiError> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum PushApiError {
#[error("Unrecognized UAID: {0}")]
UAIDNotRecognizedError(String),
#[error("No record for chid {0}")]
RecordNotFoundError(String),
#[error("Internal Error: {0}")]
InternalError(String),
}
#[derive(Debug, thiserror::Error)]
pub enum PushError {
#[error("General Error: {0:?}")]
GeneralError(String),
#[error("Crypto error: {0}")]
CryptoError(String),
#[error("Communication Error: {0:?}")]
CommunicationError(String),
#[error("Communication Server Error: {0}")]
CommunicationServerError(String),
#[error("Channel already registered.")]
AlreadyRegisteredError,
#[error("Storage Error: {0:?}")]
StorageError(String),
#[error("No record for chid {0:?}")]
RecordNotFoundError(String),
#[error("Error executing SQL: {0}")]
StorageSqlError(#[from] rusqlite::Error),
#[error("Transcoding Error: {0}")]
TranscodingError(String),
#[error("URL parse error: {0:?}")]
UrlParseError(#[from] url::ParseError),
#[error("Failed to parse json: {0}")]
JSONDeserializeError(#[from] serde_json::Error),
#[error("Unrecognized UAID: {0}")]
UAIDNotRecognizedError(String),
#[error("Unable to send request to server: {0}")]
RequestError(#[from] viaduct::Error),
#[error("Error opening database: {0}")]
OpenDatabaseError(#[from] sql_support::open_database::Error),
}
impl From<bincode::Error> for PushError {
fn from(value: bincode::Error) -> Self {
PushError::TranscodingError(format!("bincode error: {value}"))
}
}
impl From<base64::DecodeError> for PushError {
fn from(value: base64::DecodeError) -> Self {
PushError::TranscodingError(format!("base64 error: {value}"))
}
}
impl From<rc_crypto::ece::Error> for PushError {
fn from(value: rc_crypto::ece::Error) -> Self {
PushError::CryptoError(value.to_string())
}
}
impl GetErrorHandling for PushError {
type ExternalError = PushApiError;
fn get_error_handling(&self) -> error_support::ErrorHandling<Self::ExternalError> {
match self {
Self::UAIDNotRecognizedError(s) => {
ErrorHandling::convert(PushApiError::UAIDNotRecognizedError(s.clone()))
.report_error("uaid-not-recognized")
}
Self::RecordNotFoundError(s) => {
ErrorHandling::convert(PushApiError::RecordNotFoundError(s.clone()))
}
_ => ErrorHandling::convert(PushApiError::InternalError(self.to_string())),
}
}
}