use crate::domain::DomainError;
use neo4rs::{Error as Neo4jError, Neo4jErrorKind};
use std::error::Error as StdError;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Domain(#[from] DomainError),
#[error(transparent)]
Neo4j(#[from] Neo4jError),
#[error("Neo4j row decoding failed: {0}")]
Neo4jDecode(#[from] neo4rs::DeError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Dotenv(#[from] dotenvy::Error),
#[error(transparent)]
TomlDeserialize(#[from] toml::de::Error),
#[error(transparent)]
TomlSerialize(#[from] toml::ser::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Http(#[from] reqwest::Error),
#[error(
"{provider} embedding request failed with HTTP {status}: {body} (request_id={request_id:?})"
)]
EmbeddingHttp {
provider: &'static str,
status: u16,
request_id: Option<String>,
body: String,
},
#[error("{0}")]
Configuration(String),
#[error("{0}")]
Embedding(String),
#[error("{0}")]
EmbeddingSpace(String),
#[error("{0}")]
Graph(String),
#[error("{0}")]
Operation(String),
#[error("concurrent mutation changed {0}")]
ConcurrentMutation(String),
#[error("{operation} exceeded its pre-commit deadline; no changes were committed")]
MutationTimeout {
operation: &'static str,
},
#[error("{operation} commit result could not be confirmed: {source}")]
AmbiguousCommit {
operation: &'static str,
#[source]
source: Neo4jError,
},
#[error("{operation} commit result could not be confirmed: {message}")]
CommitOutcomeUnknown {
operation: &'static str,
message: String,
},
#[error("{operation} committed, but its response could not be produced: {source}")]
MutationApplied {
operation: &'static str,
#[source]
source: Box<Error>,
},
#[error("{message}: {source}")]
Context {
message: String,
#[source]
source: Box<dyn StdError + Send + Sync>,
},
}
impl Error {
pub fn context(self, message: impl Into<String>) -> Self {
Self::Context {
message: message.into(),
source: Box::new(self),
}
}
pub fn is_transient_neo4j(&self) -> bool {
if matches!(
self,
Self::AmbiguousCommit { .. }
| Self::CommitOutcomeUnknown { .. }
| Self::MutationApplied { .. }
) {
return false;
}
let mut current: Option<&(dyn StdError + 'static)> = Some(self);
while let Some(error) = current {
if error.downcast_ref::<Error>().is_some_and(|error| {
matches!(
error,
Error::AmbiguousCommit { .. }
| Error::CommitOutcomeUnknown { .. }
| Error::MutationApplied { .. }
)
}) {
return false;
}
let direct = error.downcast_ref::<Neo4jError>();
let wrapped = error.downcast_ref::<Error>().and_then(|error| match error {
Error::Neo4j(driver) => Some(driver),
_ => None,
});
if direct.or(wrapped).is_some_and(neo4j_is_transient) {
return true;
}
current = error.source();
}
false
}
}
fn neo4j_is_transient(error: &Neo4jError) -> bool {
matches!(error, Neo4jError::Neo4j(error) if error.kind() == Neo4jErrorKind::Transient)
}
pub trait Context<T> {
fn context(self, message: impl Into<String>) -> Result<T>;
fn with_context<F>(self, message: F) -> Result<T>
where
F: FnOnce() -> String;
}
impl<T, E> Context<T> for std::result::Result<T, E>
where
E: StdError + Send + Sync + 'static,
{
fn context(self, message: impl Into<String>) -> Result<T> {
self.map_err(|source| Error::Context {
message: message.into(),
source: Box::new(source),
})
}
fn with_context<F>(self, message: F) -> Result<T>
where
F: FnOnce() -> String,
{
self.map_err(|source| Error::Context {
message: message(),
source: Box::new(source),
})
}
}
#[macro_export]
macro_rules! config_error {
($($arg:tt)*) => {
$crate::Error::Configuration(format!($($arg)*))
};
}
#[macro_export]
macro_rules! embedding_error {
($($arg:tt)*) => {
$crate::Error::Embedding(format!($($arg)*))
};
}
#[macro_export]
macro_rules! graph_error {
($($arg:tt)*) => {
$crate::Error::Graph(format!($($arg)*))
};
}
#[macro_export]
macro_rules! operation_error {
($($arg:tt)*) => {
$crate::Error::Operation(format!($($arg)*))
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn context_retains_typed_sources() {
let error = std::fs::read("/definitely/not/mindreader")
.context("read fixture")
.unwrap_err();
assert!(matches!(error, Error::Context { .. }));
assert!(error
.source()
.and_then(|source| source.downcast_ref::<std::io::Error>())
.is_some());
}
#[test]
fn error_context_retains_the_typed_error_chain() {
let error = Error::from(std::io::Error::other("offline")).context("after retries");
let wrapped = error.source().expect("typed application error");
assert!(matches!(
wrapped.downcast_ref::<Error>(),
Some(Error::Io(_))
));
}
#[test]
fn untyped_driver_messages_are_not_retryable() {
let error = Error::Neo4j(Neo4jError::UnexpectedMessage(
"FAILURE code=Neo.TransientError.Transaction.DeadlockDetected".into(),
));
assert!(!error.is_transient_neo4j());
}
}