use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
InvalidCursor,
FieldNotFound,
Filter,
Sort,
Search,
Validation,
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum CoreError {
#[error("invalid cursor: {reason}")]
InvalidCursor {
reason: String,
},
#[error("field not found: {field}")]
FieldNotFound {
field: String,
},
#[error("filter error: {message}")]
Filter {
message: String,
},
#[error("sort error: {message}")]
Sort {
message: String,
},
#[error("search error: {message}")]
Search {
message: String,
},
#[error("{message}")]
Validation {
message: String,
},
}
impl CoreError {
#[must_use]
pub fn kind(&self) -> ErrorKind {
match self {
Self::InvalidCursor { .. } => ErrorKind::InvalidCursor,
Self::FieldNotFound { .. } => ErrorKind::FieldNotFound,
Self::Filter { .. } => ErrorKind::Filter,
Self::Sort { .. } => ErrorKind::Sort,
Self::Search { .. } => ErrorKind::Search,
Self::Validation { .. } => ErrorKind::Validation,
}
}
}
pub type Result<T> = std::result::Result<T, CoreError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_strings_are_stable() {
let e = CoreError::InvalidCursor {
reason: "base64".into(),
};
assert_eq!(e.to_string(), "invalid cursor: base64");
let v = CoreError::Validation {
message: "limit must be >= 1".into(),
};
assert_eq!(v.to_string(), "limit must be >= 1");
}
#[test]
fn kind_is_stable_per_variant() {
assert_eq!(
CoreError::Filter {
message: String::new()
}
.kind(),
ErrorKind::Filter
);
assert_eq!(
CoreError::FieldNotFound {
field: String::new()
}
.kind(),
ErrorKind::FieldNotFound
);
}
}