use super::*;
use thiserror::Error;
pub type DsnpGraphResult<T> = std::result::Result<T, DsnpGraphError>;
#[repr(u8)]
#[derive(Debug, Error)]
pub enum DsnpGraphError {
#[error(transparent)]
AvroError(#[from] apache_avro::Error),
#[error("Add of duplicate connection in another page detected")]
DuplicateConnectionDetected,
#[error("Calling apply_prids in non private friendship graph!")]
CallToPridsInPublicGraph,
#[error("Call to private friends in non private graph")]
CallToPrivateFriendsInPublicGraph,
#[error("Connection from {0} to {1} already exists!")]
ConnectionAlreadyExists(DsnpUserId, DsnpUserId),
#[error("Connection from {0} to {1} does not exist!")]
ConnectionDoesNotExist(DsnpUserId, DsnpUserId),
#[error("Connection not found")]
ConnectionNotFound,
#[error("Failed to decompress: {0}")]
DecompressError(String),
#[error("Failed to decrypt: {0}")]
DecryptionError(String),
#[error("Duplicate update events detected")]
DuplicateUpdateEvents,
#[error("Event exists")]
EventExists,
#[error("Failed to encrypt: {0}")]
EncryptionError(String),
#[error("Failed to retrieve graph page")]
FailedToRetrieveGraphPage,
#[error("Failed to acquire read lock on {0}")]
FailedtoReadLock(String),
#[error("Failed to acquire write lock on {0}")]
FailedtoWriteLock(String),
#[error("FFI error: {0}")]
FFIError(String),
#[error("Graph is full")]
GraphIsFull,
#[error("Invalid user id: {0}")]
InvalidDsnpUserId(DsnpUserId),
#[error("Invalid schema id: {0}")]
InvalidSchemaId(SchemaId),
#[error("Invalid Page ID: {0}")]
InvalidPageId(PageId),
#[error("Invalid private schema id")]
InvalidPrivateSchemaId,
#[error("Invalid public key")]
InvalidPublicKey,
#[error("Invalid secret key")]
InvalidSecretKey,
#[error("{0}")]
InvalidInput(String),
#[error("Imported key not found for user {0} and id {1}")]
ImportedKeyNotFound(DsnpUserId, String),
#[error("Incorrect connection type: {0}")]
IncorrectConnectionType(String),
#[error("Incompatible privacy type for blob export")]
IncompatiblePrivacyTypeForBlobExport,
#[error("Key derivation error: {0}")]
KeyDerivationError(String),
#[error("No pris imported for user: {0}")]
NoPrisImportedForUser(DsnpUserId),
#[error("No public key found for user: {0}")]
NoPublicKeyFoundForUser(DsnpUserId),
#[error("No resolved active key found")]
NoResolvedActiveKeyFound,
#[error("New page for existing page id")]
NewPageForExistingPageId,
#[error("Page is aggressively full")]
PageAggressivelyFull,
#[error("Page is trivially full")]
PageTriviallyFull,
#[error("Given public key already exists: {0}")]
PublicKeyAlreadyExists(String),
#[error("Public key not compatible with secret key")]
PublicKeyNotCompatibleWithSecretKey,
#[error(
"page_id: {0}, prids len should be equal to connections len (connections: {1}, prids: {2})"
)]
PridsLenShouldBeEqualToConnectionsLen(PageId, usize, usize),
#[error("Unsupported schema: {0}")]
UnsupportedSchema(SchemaId),
#[error(transparent)]
Unknown(#[from] anyhow::Error),
#[error("User graph for {0} is not imported")]
UserGraphNotImported(DsnpUserId),
#[error("Unable to decrypt private graph with any of the imported keys")]
UnableToDecryptGraphChunkWithAnyKey,
#[error("No schema ID found for connection type")]
UnsupportedConnectionTypeForConfig(ConnectionType),
}
impl DsnpGraphError {
pub fn error_code(&self) -> i32 {
match self {
DsnpGraphError::AvroError { .. } => 1,
DsnpGraphError::DuplicateConnectionDetected => 2,
DsnpGraphError::CallToPridsInPublicGraph => 3,
DsnpGraphError::CallToPrivateFriendsInPublicGraph => 4,
DsnpGraphError::ConnectionAlreadyExists(..) => 5,
DsnpGraphError::ConnectionDoesNotExist(..) => 6,
DsnpGraphError::ConnectionNotFound => 7,
DsnpGraphError::DecompressError(_) => 8,
DsnpGraphError::DecryptionError(_) => 9,
DsnpGraphError::DuplicateUpdateEvents => 10,
DsnpGraphError::EventExists => 11,
DsnpGraphError::EncryptionError(_) => 12,
DsnpGraphError::FailedToRetrieveGraphPage => 13,
DsnpGraphError::FailedtoReadLock(_) => 14,
DsnpGraphError::FailedtoWriteLock(_) => 15,
DsnpGraphError::GraphIsFull => 16,
DsnpGraphError::InvalidDsnpUserId(_) => 17,
DsnpGraphError::InvalidSchemaId(_) => 20,
DsnpGraphError::InvalidPageId(_) => 21,
DsnpGraphError::InvalidPrivateSchemaId => 22,
DsnpGraphError::InvalidPublicKey => 23,
DsnpGraphError::InvalidSecretKey => 24,
DsnpGraphError::InvalidInput(_) => 25,
DsnpGraphError::ImportedKeyNotFound(..) => 26,
DsnpGraphError::IncorrectConnectionType(_) => 27,
DsnpGraphError::IncompatiblePrivacyTypeForBlobExport => 28,
DsnpGraphError::KeyDerivationError(_) => 29,
DsnpGraphError::NoPrisImportedForUser(_) => 30,
DsnpGraphError::NoPublicKeyFoundForUser(_) => 31,
DsnpGraphError::NoResolvedActiveKeyFound => 32,
DsnpGraphError::NewPageForExistingPageId => 33,
DsnpGraphError::PageAggressivelyFull => 34,
DsnpGraphError::PageTriviallyFull => 35,
DsnpGraphError::PublicKeyAlreadyExists(_) => 36,
DsnpGraphError::PublicKeyNotCompatibleWithSecretKey => 37,
DsnpGraphError::PridsLenShouldBeEqualToConnectionsLen(..) => 38,
DsnpGraphError::UnsupportedSchema(_) => 39,
DsnpGraphError::Unknown(..) => 40,
DsnpGraphError::UserGraphNotImported(_) => 41,
DsnpGraphError::UnableToDecryptGraphChunkWithAnyKey => 42,
DsnpGraphError::FFIError(_) => 43,
DsnpGraphError::UnsupportedConnectionTypeForConfig(..) => 44,
}
}
}
#[macro_export]
macro_rules! ok_or_log {
($target:expr, $error:expr, $level:expr) => {{
$target.ok_or_else(|| {
log::log!($level, "{}", $error);
$error
})
}};
($target:expr, $error:expr, $level:expr, $context:expr) => {{
$target.ok_or_else(|| {
log::log!($level, "{}: {}", $context, $error);
$error
})
}};
}
#[macro_export]
macro_rules! log_err {
($target:expr) => {{
let r = $target;
if let Some(err) = r.as_ref().err() {
log::error!("{}", err);
}
r
}};
($target:expr, $context:expr) => {{
let r = $target;
if let Some(err) = r.as_ref().err() {
log::error!("{}:\n{}", $context, err);
}
r
}};
}