use std::fmt;
#[derive(Debug, Clone)]
pub enum Error {
Native { code: i32, detail: String },
OutcomeUnknownWithReceipt {
detail: Option<String>,
receipt: String,
},
InvalidArgument,
NotFound,
VersionConflict,
IntentConflict,
TxnNotFound,
Busy,
OutcomeUnknown,
Unsupported,
Stalled,
Cancelled,
ReplacementFailed,
ReplacementPending,
ReplacementConfigurationMismatch,
Internal,
Unknown(i32),
}
impl Error {
pub fn kind(&self) -> Error {
Error::from_code(self.code())
}
pub fn detail(&self) -> Option<&str> {
match self {
Error::Native { detail, .. } => Some(detail),
Error::OutcomeUnknownWithReceipt { detail, .. } => detail.as_deref(),
_ => None,
}
}
pub fn outcome_receipt(&self) -> Option<&str> {
match self {
Error::OutcomeUnknownWithReceipt { receipt, .. } => Some(receipt),
_ => None,
}
}
pub(crate) fn with_outcome_receipt(self, receipt: Option<String>) -> Error {
match receipt {
Some(receipt) if self.code() == 7 => Error::OutcomeUnknownWithReceipt {
detail: self.detail().map(str::to_owned),
receipt,
},
_ => self,
}
}
pub(crate) fn from_native_with_receipt(code: i32) -> Error {
let receipt = if code == 7 {
last_outcome_receipt()
} else {
None
};
Error::from_native(code).with_outcome_receipt(receipt)
}
pub(crate) fn from_native(code: i32) -> Error {
#[cfg(feature = "libantfly")]
if unsafe { antfly_embedded_sys::antfly_last_error_code() } == code {
let detail = last_error_detail();
if !detail.is_empty() {
return Error::Native { code, detail };
}
}
Error::from_code(code)
}
pub fn from_code(code: i32) -> Error {
match code {
1 => Error::InvalidArgument,
2 => Error::NotFound,
3 => Error::VersionConflict,
4 => Error::IntentConflict,
5 => Error::TxnNotFound,
6 => Error::Busy,
7 => Error::OutcomeUnknown,
8 => Error::Unsupported,
9 => Error::Stalled,
11 => Error::ReplacementFailed,
12 => Error::ReplacementPending,
13 => Error::ReplacementConfigurationMismatch,
10 => Error::Cancelled,
255 => Error::Internal,
other => Error::Unknown(other),
}
}
pub fn code(&self) -> i32 {
match self {
Error::Native { code, .. } => *code,
Error::OutcomeUnknownWithReceipt { .. } => 7,
Error::InvalidArgument => 1,
Error::NotFound => 2,
Error::VersionConflict => 3,
Error::IntentConflict => 4,
Error::TxnNotFound => 5,
Error::Busy => 6,
Error::OutcomeUnknown => 7,
Error::Unsupported => 8,
Error::Stalled => 9,
Error::ReplacementFailed => 11,
Error::ReplacementPending => 12,
Error::ReplacementConfigurationMismatch => 13,
Error::Cancelled => 10,
Error::Internal => 255,
Error::Unknown(code) => *code,
}
}
pub fn name(&self) -> &'static str {
match self {
Error::InvalidArgument => "ANTFLY_INVALID_ARGUMENT",
Error::NotFound => "ANTFLY_NOT_FOUND",
Error::VersionConflict => "ANTFLY_VERSION_CONFLICT",
Error::IntentConflict => "ANTFLY_INTENT_CONFLICT",
Error::TxnNotFound => "ANTFLY_TXN_NOT_FOUND",
Error::Busy => "ANTFLY_BUSY",
Error::OutcomeUnknown => "ANTFLY_OUTCOME_UNKNOWN",
Error::Unsupported => "ANTFLY_UNSUPPORTED",
Error::Stalled => "ANTFLY_STALLED",
Error::ReplacementFailed => "ANTFLY_REPLACEMENT_FAILED",
Error::ReplacementPending => "ANTFLY_REPLACEMENT_PENDING",
Error::ReplacementConfigurationMismatch => "ANTFLY_REPLACEMENT_CONFIGURATION_MISMATCH",
Error::Cancelled => "ANTFLY_CANCELLED",
Error::Internal => "ANTFLY_INTERNAL",
Error::Unknown(_) => "ANTFLY_UNKNOWN_ERROR",
Error::Native { code, .. } => Error::from_code(*code).name(),
Error::OutcomeUnknownWithReceipt { .. } => Error::OutcomeUnknown.name(),
}
}
pub fn description(&self) -> &'static str {
match self {
Error::InvalidArgument => "an argument, request, path, or open mode is invalid",
Error::NotFound => "the requested database object was not found",
Error::VersionConflict => {
"a version predicate did not match the current document version"
}
Error::IntentConflict => "a transaction intent conflicts with the requested operation",
Error::TxnNotFound => "the requested transaction was not found",
Error::Busy => {
"the requested resource is temporarily busy or changed during streaming; \
stabilize it and retry"
}
Error::OutcomeUnknown => {
"the operation may have taken effect, but whether it applied or became durable \
could not be confirmed; inspect the affected data and do not retry automatically"
}
Error::Unsupported => {
"the operation requires a capability that is not supported by this platform \
or filesystem"
}
Error::Stalled => {
"derived work stalled or failed; inspect pending-work diagnostics and \
repair or reopen the database"
}
Error::ReplacementFailed => {
"replacement failed after its retry budget; inspect diagnostics and cancel before retrying"
}
Error::ReplacementPending => "index name is reserved by a pending replacement",
Error::ReplacementConfigurationMismatch => {
"replacement configuration differs from the reserved request"
}
Error::Cancelled => "the caller cancelled the operation",
Error::Internal => "an internal error occurred",
Error::Unknown(_) => "unknown Antfly error code",
Error::Native { code, .. } => Error::from_code(*code).description(),
Error::OutcomeUnknownWithReceipt { .. } => Error::OutcomeUnknown.description(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.name(), self.description())?;
if let Some(detail) = self.detail() {
write!(f, ": {detail}")?;
}
if let Some(receipt) = self.outcome_receipt() {
write!(f, " (outcome receipt {receipt})")?;
}
Ok(())
}
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
self.code() == other.code()
}
}
impl Eq for Error {}
impl std::hash::Hash for Error {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(&self.code(), state);
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(feature = "libantfly")]
pub fn last_error_detail() -> String {
unsafe {
std::ffi::CStr::from_ptr(antfly_embedded_sys::antfly_last_error_detail())
.to_string_lossy()
.into_owned()
}
}
fn last_outcome_receipt() -> Option<String> {
#[cfg(feature = "libantfly")]
{
let mut out = antfly_embedded_sys::antfly_buffer::default();
let code = unsafe { antfly_embedded_sys::antfly_last_error_outcome_receipt(&mut out) };
if code != antfly_embedded_sys::ANTFLY_OK {
return None;
}
let bytes = unsafe { crate::ffi::take_buffer(out) };
if bytes.is_empty() {
return None;
}
Some(String::from_utf8_lossy(&bytes).into_owned())
}
#[cfg(not(feature = "libantfly"))]
None
}