use super::{CrossingError, MongoSortError};
use crate::{CursorError, comparison::mongo::MongoLoweringError};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MongoTypeError {
Absent,
Type {
wanted: &'static str,
found: &'static str,
},
Range(&'static str),
}
impl MongoTypeError {
#[inline]
#[must_use]
pub const fn wrong_type(wanted: &'static str, found: &'static str) -> Self {
Self::Type { wanted, found }
}
}
impl core::fmt::Display for MongoTypeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Absent => f.write_str("this field is not in the document"),
Self::Type { wanted, found } => {
write!(f, "this field holds a {found} where a {wanted} was stored")
}
Self::Range(why) => f.write_str(why),
}
}
}
impl core::error::Error for MongoTypeError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MongoExtractionError {
field: &'static str,
cause: MongoTypeError,
}
impl MongoExtractionError {
#[inline]
#[must_use]
pub const fn new(field: &'static str, cause: MongoTypeError) -> Self {
Self { field, cause }
}
#[inline]
#[must_use]
pub const fn field(&self) -> &'static str {
self.field
}
#[inline]
#[must_use]
pub const fn cause(&self) -> &MongoTypeError {
&self.cause
}
}
impl core::fmt::Display for MongoExtractionError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let Self { field, cause } = self;
write!(f, "`{field}` did not read: {cause}")
}
}
impl core::error::Error for MongoExtractionError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
Some(&self.cause)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum MongoQueryError {
#[cfg(feature = "mongodb")]
Driver(mongodb::error::Error),
Lowering(MongoLoweringError),
Order(MongoSortError),
Cursor(CursorError),
Skip(u64),
Extraction(MongoExtractionError),
Crossing(CrossingError),
}
#[derive(Debug)]
#[non_exhaustive]
pub enum MongoWriteError {
#[cfg(feature = "mongodb")]
Driver(mongodb::error::Error),
Lowering(MongoLoweringError),
Crossing(CrossingError),
Joined(usize),
Unscoped(&'static crate::FieldName),
}
#[cfg(feature = "mongodb")]
impl From<mongodb::error::Error> for MongoWriteError {
#[inline]
fn from(error: mongodb::error::Error) -> Self {
Self::Driver(error)
}
}
impl From<MongoLoweringError> for MongoWriteError {
#[inline]
fn from(error: MongoLoweringError) -> Self {
Self::Lowering(error)
}
}
impl From<CrossingError> for MongoWriteError {
#[inline]
fn from(error: CrossingError) -> Self {
Self::Crossing(error)
}
}
impl core::fmt::Display for MongoWriteError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
#[cfg(feature = "mongodb")]
Self::Driver(error) => write!(f, "this write did not run: {error}"),
Self::Lowering(error) => write!(f, "this write's predicate does not lower here: {error}"),
Self::Crossing(error) => write!(f, "this record and its document do not cross: {error}"),
Self::Joined(stages) => write!(
f,
"this delete's filter needs {stages} `$lookup` stage(s), and a delete command carries no \
pipeline — a predicate that reaches another collection is refused rather than run without \
it"
),
Self::Unscoped(collection) => write!(
f,
"this delete names no document, so it would empty `{}` — a filter that lowers to `{{}}` is \
refused rather than run",
collection.as_str()
),
}
}
}
impl core::error::Error for MongoWriteError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
#[cfg(feature = "mongodb")]
Self::Driver(error) => Some(error),
Self::Lowering(error) => Some(error),
Self::Crossing(error) => Some(&**error),
Self::Joined(_) | Self::Unscoped(_) => None,
}
}
}
const SKIP_CEILING: u64 = i64::MAX as u64;
#[cfg(feature = "mongodb")]
impl From<mongodb::error::Error> for MongoQueryError {
#[inline]
fn from(error: mongodb::error::Error) -> Self {
Self::Driver(error)
}
}
impl From<MongoLoweringError> for MongoQueryError {
#[inline]
fn from(error: MongoLoweringError) -> Self {
Self::Lowering(error)
}
}
impl From<MongoSortError> for MongoQueryError {
#[inline]
fn from(error: MongoSortError) -> Self {
Self::Order(error)
}
}
impl From<CursorError> for MongoQueryError {
#[inline]
fn from(error: CursorError) -> Self {
Self::Cursor(error)
}
}
impl From<MongoExtractionError> for MongoQueryError {
#[inline]
fn from(error: MongoExtractionError) -> Self {
Self::Extraction(error)
}
}
impl From<CrossingError> for MongoQueryError {
#[inline]
fn from(error: CrossingError) -> Self {
Self::Crossing(error)
}
}
impl core::fmt::Display for MongoQueryError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
#[cfg(feature = "mongodb")]
Self::Driver(error) => write!(f, "this query did not run: {error}"),
Self::Lowering(error) => write!(f, "this query's predicate does not lower here: {error}"),
Self::Order(error) => write!(f, "this query's order cannot be rendered here: {error}"),
Self::Cursor(error) => write!(f, "this query cannot resume there: {error}"),
Self::Skip(skip) => write!(
f,
"a skip of {skip} is past the {SKIP_CEILING} a `$skip` holds"
),
Self::Extraction(error) => write!(f, "this document is not a row: {error}"),
Self::Crossing(error) => write!(f, "this row and its record do not cross: {error}"),
}
}
}
impl core::error::Error for MongoQueryError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
#[cfg(feature = "mongodb")]
Self::Driver(error) => Some(error),
Self::Lowering(error) => Some(error),
Self::Order(error) => Some(error),
Self::Cursor(error) => Some(error),
Self::Skip(_) => None,
Self::Extraction(error) => Some(error),
Self::Crossing(error) => Some(&**error),
}
}
}
pub(super) fn offset(skip: u64) -> Result<i64, MongoQueryError> {
i64::try_from(skip).map_err(|_| MongoQueryError::Skip(skip))
}
#[cfg(test)]
mod tests;