use crate::record::RecordDecodeError;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ArcadeDbError {
#[error("failed to connect to ArcadeDB gRPC at {addr} after {attempts} attempts")]
Connect {
addr: String,
attempts: u32,
#[source]
source: Option<tonic::transport::Error>,
},
#[error("invalid gRPC endpoint {addr}")]
InvalidEndpoint {
addr: String,
#[source]
source: http::uri::InvalidUri,
},
#[error("{operation} failed: {status} (in: {detail})")]
Rpc {
operation: String,
detail: String,
#[source]
status: tonic::Status,
},
#[error("{what}: {message}")]
AlreadyExists {
what: String,
message: String,
},
#[error("command failed: {message}")]
CommandFailed {
operation: String,
message: String,
affected: Option<i64>,
},
#[error("decode {what}")]
Decode {
what: String,
#[source]
source: RecordDecodeError,
},
#[error("no rows returned by {operation} (expected at least one: {detail})")]
NotFound {
operation: String,
detail: String,
},
#[error("{message}")]
InvalidInput {
message: String,
},
}
impl ArcadeDbError {
pub fn code(&self) -> Option<tonic::Code> {
match self {
ArcadeDbError::Rpc { status, .. } => Some(status.code()),
_ => None,
}
}
pub fn server_message(&self) -> Option<&str> {
match self {
ArcadeDbError::Rpc { status, .. } => Some(status.message()),
ArcadeDbError::CommandFailed { message, .. } => Some(message),
ArcadeDbError::AlreadyExists { message, .. } => Some(message),
_ => None,
}
}
}
pub type Result<T, E = ArcadeDbError> = std::result::Result<T, E>;