#![deny(clippy::wildcard_enum_match_arm)]
use common::{LeafError, internal_todo};
use surrealdb_types::{Error as TypesError, SerializationError, ToSql, ValidationError};
use crate::expr::operation::PatchError;
use crate::val::{CastError, CoerceError, Value};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Cannot use {} in a CONTENT clause", value.to_sql())]
InvalidContent {
value: Value,
},
#[error("Cannot use {} in a MERGE clause", value.to_sql())]
InvalidMerge {
value: Value,
},
#[error("The JSON Patch contains invalid operations. {0}")]
InvalidPatch(PatchError),
#[error(
"Given test operation failed for JSON Patch. Expected `{expected}`, but got `{got}` instead."
)]
PatchTest {
expected: String,
got: String,
},
#[error("Incorrect arguments for function {name}(). {message}")]
InvalidFunctionArguments {
name: String,
message: String,
},
#[error("Incorrect arguments for method {name}(). {message}")]
InvalidMethodArguments {
name: String,
message: String,
},
#[error("Invalid regular expression: {0:?}")]
InvalidRegex(String),
#[error("Found {value} for the Record ID but this is not a valid id")]
IdInvalid {
value: String,
},
#[error("Found {value} for the Record ID but this is not a valid table name")]
TbInvalid {
value: String,
},
#[error("{variant} destructuring method is not supported here")]
UnsupportedDestructure {
variant: String,
},
#[error("{0}")]
Coerce(#[from] CoerceError),
#[error("{0}")]
Cast(#[from] CastError),
#[error("Cannot perform addition with '{0}' and '{1}'")]
TryAdd(String, String),
#[error("Cannot perform subtraction with '{0}' and '{1}'")]
TrySub(String, String),
#[error("Cannot perform multiplication with '{0}' and '{1}'")]
TryMul(String, String),
#[error("Cannot perform division with '{0}' and '{1}'")]
TryDiv(String, String),
#[error("Cannot perform remainder with '{0}' and '{1}'")]
TryRem(String, String),
#[error("Cannot raise the value '{0}' with '{1}'")]
TryPow(String, String),
#[error("Cannot negate the value '{0}'")]
TryNeg(String),
#[error("Cannot extend '{0}' as it is not an array")]
TryExtend(String),
#[error("Cannot convert from '{0}' to '{1}'")]
TryFrom(String, &'static str),
#[error("Timestamp arithmetic error: {0}")]
TimestampOverflow(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Failed to compute: \"{0}\", as the operation results in an arithmetic overflow.")]
ArithmeticOverflow(String),
#[error("Failed to compute: \"{0}\", as the operation results in a negative value.")]
ArithmeticNegativeOverflow(String),
#[error("The string could not be parsed into a bytesize")]
InvalidBytesize,
}
impl LeafError for Error {
fn map_kind(self, message: String) -> TypesError {
match self {
Error::InvalidContent {
value,
} => TypesError::validation(
message,
ValidationError::InvalidContent {
value: value.to_sql(),
},
),
Error::InvalidMerge {
value,
} => TypesError::validation(
message,
ValidationError::InvalidMerge {
value: value.to_sql(),
},
),
Error::InvalidPatch(_) => TypesError::validation(message, None),
Error::Coerce(_) => TypesError::validation(message, None),
Error::Cast(_) => TypesError::validation(message, None),
Error::TryAdd(..)
| Error::TrySub(..)
| Error::TryMul(..)
| Error::TryDiv(..)
| Error::TryRem(..)
| Error::TryPow(..)
| Error::TryNeg(_)
| Error::TryExtend(_) => TypesError::validation(message, None),
Error::TryFrom(..) => TypesError::validation(message, None),
Error::Serialization(..) => {
TypesError::serialization(message, SerializationError::Serialization)
}
Error::TimestampOverflow(..) => TypesError::internal(message),
Error::PatchTest {
..
}
| Error::InvalidFunctionArguments {
..
}
| Error::InvalidMethodArguments {
..
}
| Error::InvalidRegex(_)
| Error::IdInvalid {
..
}
| Error::TbInvalid {
..
}
| Error::UnsupportedDestructure {
..
}
| Error::ArithmeticOverflow(_)
| Error::ArithmeticNegativeOverflow(_)
| Error::InvalidBytesize => internal_todo(message),
}
}
}
impl Error {
pub fn is_ignorable(&self) -> bool {
matches!(
self,
Error::Coerce(_)
| Error::Cast(_)
| Error::InvalidFunctionArguments { .. }
| Error::TryAdd(..)
| Error::TrySub(..)
| Error::TryMul(..)
| Error::TryDiv(..)
| Error::TryPow(..)
| Error::TryNeg(..)
| Error::TryExtend(..)
)
}
}
impl From<regex::Error> for Error {
fn from(error: regex::Error) -> Self {
Error::InvalidRegex(error.to_string())
}
}