Add fail!() macro and test (#4699)

Co-authored-by: Tobie Morgan Hitchcock <tobie@surrealdb.com>
This commit is contained in:
Raphael Darley 2024-09-08 23:39:42 +01:00 committed by GitHub
parent bb1884d56b
commit 5da3a642a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 2 deletions

View file

@ -88,7 +88,7 @@ impl Document {
// two separtate UNIQUE index definitions, and it // two separtate UNIQUE index definitions, and it
// wasn't possible to detect which record was the // wasn't possible to detect which record was the
// correct one to be updated // correct one to be updated
let _ = chn.send(Err(Error::Unreachable("Internal error"))).await; let _ = chn.send(Err(fail!("Internal error"))).await;
// Break the loop // Break the loop
Ok(()) Ok(())
} }

View file

@ -80,6 +80,6 @@ impl Document {
// two separtate UNIQUE index definitions, and it // two separtate UNIQUE index definitions, and it
// wasn't possible to detect which record was the // wasn't possible to detect which record was the
// correct one to be updated // correct one to be updated
Err(Error::Unreachable("Internal error")) Err(fail!("Internal error"))
} }
} }

View file

@ -1,3 +1,10 @@
/// Throws an unreachable error with location details
macro_rules! fail {
($msg: literal) => {
$crate::err::Error::Unreachable(concat!(file!(), ":", line!(), ": ", $msg))
};
}
/// Converts some text into a new line byte string /// Converts some text into a new line byte string
macro_rules! bytes { macro_rules! bytes {
($expression:expr) => { ($expression:expr) => {
@ -203,6 +210,8 @@ macro_rules! async_defer{
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::err::Error;
#[tokio::test] #[tokio::test]
async fn async_defer_basic() { async fn async_defer_basic() {
let mut counter = 0; let mut counter = 0;
@ -241,4 +250,12 @@ mod test {
}) })
.await; .await;
} }
#[test]
fn fail() {
let Error::Unreachable(msg) = fail!("Reached unreachable code") else {
panic!()
};
assert_eq!("core/src/mac/mod.rs:256: Reached unreachable code", msg);
}
} }