use thiserror::Error;
pub type Result<T> = std::result::Result<T, YugenDbError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ErrorCode {
NotFound,
ConnectionError,
SerialisationError,
DeserialisationError,
Timeout,
Conflict,
TransactionAborted,
UnsupportedFeature,
ConstraintViolation,
InvalidKey,
InvalidNamespace,
InvalidCollection,
InvalidValue,
DriverError,
InternalError,
}
impl ErrorCode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::NotFound => "NOT_FOUND",
Self::ConnectionError => "CONNECTION_ERROR",
Self::SerialisationError => "SERIALISATION_ERROR",
Self::DeserialisationError => "DESERIALISATION_ERROR",
Self::Timeout => "TIMEOUT",
Self::Conflict => "CONFLICT",
Self::TransactionAborted => "TRANSACTION_ABORTED",
Self::UnsupportedFeature => "UNSUPPORTED_FEATURE",
Self::ConstraintViolation => "CONSTRAINT_VIOLATION",
Self::InvalidKey => "INVALID_KEY",
Self::InvalidNamespace => "INVALID_NAMESPACE",
Self::InvalidCollection => "INVALID_COLLECTION",
Self::InvalidValue => "INVALID_VALUE",
Self::DriverError => "DRIVER_ERROR",
Self::InternalError => "INTERNAL_ERROR",
}
}
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Debug, Error)]
pub enum YugenDbError {
#[error("record was not found: {message}")]
NotFound { message: String },
#[error("connection error: {message}")]
ConnectionError { message: String },
#[error("serialisation error: {message}")]
SerialisationError { message: String },
#[error("deserialisation error: {message}")]
DeserialisationError { message: String },
#[error("operation timed out: {message}")]
Timeout { message: String },
#[error("conflict: {message}")]
Conflict { message: String },
#[error("transaction aborted: {message}")]
TransactionAborted { message: String },
#[error("unsupported feature `{feature}`: {message}")]
UnsupportedFeature { feature: String, message: String },
#[error("constraint violation: {message}")]
ConstraintViolation { message: String },
#[error("invalid key: {message}")]
InvalidKey { message: String },
#[error("invalid namespace: {message}")]
InvalidNamespace { message: String },
#[error("invalid collection: {message}")]
InvalidCollection { message: String },
#[error("invalid value: {message}")]
InvalidValue { message: String },
#[error("driver error from `{driver}`: {message}")]
DriverError { driver: String, message: String },
#[error("internal error: {message}")]
InternalError { message: String },
}
impl YugenDbError {
#[must_use]
pub const fn code(&self) -> ErrorCode {
match self {
Self::NotFound { .. } => ErrorCode::NotFound,
Self::ConnectionError { .. } => ErrorCode::ConnectionError,
Self::SerialisationError { .. } => ErrorCode::SerialisationError,
Self::DeserialisationError { .. } => ErrorCode::DeserialisationError,
Self::Timeout { .. } => ErrorCode::Timeout,
Self::Conflict { .. } => ErrorCode::Conflict,
Self::TransactionAborted { .. } => ErrorCode::TransactionAborted,
Self::UnsupportedFeature { .. } => ErrorCode::UnsupportedFeature,
Self::ConstraintViolation { .. } => ErrorCode::ConstraintViolation,
Self::InvalidKey { .. } => ErrorCode::InvalidKey,
Self::InvalidNamespace { .. } => ErrorCode::InvalidNamespace,
Self::InvalidCollection { .. } => ErrorCode::InvalidCollection,
Self::InvalidValue { .. } => ErrorCode::InvalidValue,
Self::DriverError { .. } => ErrorCode::DriverError,
Self::InternalError { .. } => ErrorCode::InternalError,
}
}
#[must_use]
pub fn not_found(message: impl Into<String>) -> Self {
Self::NotFound {
message: message.into(),
}
}
#[must_use]
pub fn connection_error(message: impl Into<String>) -> Self {
Self::ConnectionError {
message: message.into(),
}
}
#[must_use]
pub fn serialisation_error(message: impl Into<String>) -> Self {
Self::SerialisationError {
message: message.into(),
}
}
#[must_use]
pub fn deserialisation_error(message: impl Into<String>) -> Self {
Self::DeserialisationError {
message: message.into(),
}
}
#[must_use]
pub fn timeout(message: impl Into<String>) -> Self {
Self::Timeout {
message: message.into(),
}
}
#[must_use]
pub fn conflict(message: impl Into<String>) -> Self {
Self::Conflict {
message: message.into(),
}
}
#[must_use]
pub fn transaction_aborted(message: impl Into<String>) -> Self {
Self::TransactionAborted {
message: message.into(),
}
}
#[must_use]
pub fn unsupported_feature(feature: impl Into<String>, message: impl Into<String>) -> Self {
Self::UnsupportedFeature {
feature: feature.into(),
message: message.into(),
}
}
#[must_use]
pub fn constraint_violation(message: impl Into<String>) -> Self {
Self::ConstraintViolation {
message: message.into(),
}
}
#[must_use]
pub fn invalid_key(message: impl Into<String>) -> Self {
Self::InvalidKey {
message: message.into(),
}
}
#[must_use]
pub fn invalid_namespace(message: impl Into<String>) -> Self {
Self::InvalidNamespace {
message: message.into(),
}
}
#[must_use]
pub fn invalid_collection(message: impl Into<String>) -> Self {
Self::InvalidCollection {
message: message.into(),
}
}
#[must_use]
pub fn invalid_value(message: impl Into<String>) -> Self {
Self::InvalidValue {
message: message.into(),
}
}
#[must_use]
pub fn driver_error(driver: impl Into<String>, message: impl Into<String>) -> Self {
Self::DriverError {
driver: driver.into(),
message: message.into(),
}
}
#[must_use]
pub fn internal_error(message: impl Into<String>) -> Self {
Self::InternalError {
message: message.into(),
}
}
}
impl From<std::convert::Infallible> for YugenDbError {
fn from(error: std::convert::Infallible) -> Self {
match error {}
}
}