Skip to content

Instantly share code, notes, and snippets.

@gbmor gbmor/errors.rs
Created Sep 7, 2019

Embed
What would you like to do?
Central Error Handler
pub fn helper<T, V>(res: Result<T, V>) -> T
where
V: std::fmt::Debug,
{
match res {
Ok(val) => val,
Err(err) => {
// This is where errors will be handled.
// Change this accordingly.
log::error!("{:?}", err);
panic!("{:?}", err);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shouldnt_panic() {
let ok: Result<&str, &str> = Ok("okay");
let rhs = helper(ok);
assert_eq!("okay", rhs);
}
#[test]
#[should_panic]
fn should_panic() {
let err: Result<&str, &str> = Err("oops");
helper(err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.