feat: macros for reducing error boilerplate
This commit is contained in:
@@ -119,7 +119,7 @@ pub enum ErrorType {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! create_error {
|
||||
( $error:ident $( $tt:tt )? ) => {
|
||||
( $error: ident $( $tt:tt )? ) => {
|
||||
$crate::Error {
|
||||
error_type: $crate::ErrorType::$error $( $tt )?,
|
||||
location: format!("{}:{}:{}", file!(), line!(), column!()),
|
||||
@@ -129,7 +129,7 @@ macro_rules! create_error {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! create_database_error {
|
||||
( $operation:expr, $collection:expr ) => {
|
||||
( $operation: expr, $collection: expr ) => {
|
||||
create_error!(DatabaseError {
|
||||
operation: $operation.to_string(),
|
||||
collection: $collection.to_string()
|
||||
@@ -137,6 +137,23 @@ macro_rules! create_database_error {
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[cfg(debug_assertions)]
|
||||
macro_rules! query {
|
||||
( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => {
|
||||
Ok($self.$type($collection, $($rest),+).await.unwrap())
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
#[cfg(not(debug_assertions))]
|
||||
macro_rules! query {
|
||||
( $self: ident, $type: ident, $collection: expr, $($rest:expr),+ ) => {
|
||||
$self.$type($collection, $($rest),+).await
|
||||
.map_err(|_| create_database_error!(stringify!($type), $collection))
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::ErrorType;
|
||||
|
||||
Reference in New Issue
Block a user