Created
April 4, 2017 00:35
-
-
Save fschutt/fd2de5010e160916125d252f6519ac76 to your computer and use it in GitHub Desktop.
Error handling in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Everything that could go wrong is in errors::Error, for error generalization | |
//! *NOTE* : In order to use the macros, you must include the error module before the | |
//! module that uses the macros | |
//! This is why in /src/lib.rs the error module is the first module listed | |
use std::fmt::Display; | |
use std::fmt::Formatter; | |
use std::fmt; | |
/// Macro for returning {1} if {0} doesn't match Some(0) | |
#[macro_export] | |
macro_rules! try_opt( | |
($e:expr, $f:expr) => (match $e { Some(e) => e, None => return $f }) | |
); | |
/// Macro for returning Err({1}) if {0} doesn't match Some(0) | |
#[macro_export] | |
macro_rules! try_opt_err( | |
($e:expr, $f:expr) => (match $e { Some(e) => e, None => return Err($f) }) | |
); | |
/// Macro for returning {1} if {0} gives any error | |
#[macro_export] | |
macro_rules! try_res( | |
($e:expr, $f:expr) => (match $e { Ok(e) => e, Err(_) => return $f }) | |
); | |
/// Macro for returning Err({1}) if {0} gives any error | |
#[macro_export] | |
macro_rules! try_res_err( | |
($e:expr, $f:expr) => (match $e { Ok(e) => e, Err(_) => return Err($f) }) | |
); | |
/// Macro for printing the error if the function fails | |
#[macro_export] | |
macro_rules! print_on_err ( | |
($e:expr) => (match $e { Ok(e) => e, Err(err) => println!("Error: {}", err) }) | |
); | |
/// Macro for printing the error if the function fails | |
#[macro_export] | |
macro_rules! print_dbg_err ( | |
($e:expr) => (match $e { Ok(e) => e, Err(err) => println!("Error: {:?}", err) }) | |
); | |
/// Macro for printing a custom message on error, plus the original error | |
#[macro_export] | |
macro_rules! print_err ( | |
($e:expr, $f:expr) => (match $e { Ok(e) => e, Err(err) => println!("Error: {}\r\nOriginated from: {}", f, err) }) | |
); | |
/// Enum containing all possible errors | |
#[derive(Debug, Clone)] | |
pub enum Error { | |
/// Generic error, unknown | |
GenericError, | |
// --- DATABASE | |
/// Database connection is invalid | |
DatabaseConnectionFailed(String), | |
// etc. | |
} | |
impl Display for Error { | |
/// Implement all error strings for user-facing output | |
/// TODO: add more error information, reformat this a bit with write!() | |
fn fmt(&self, | |
f: &mut Formatter) | |
-> fmt::Result | |
{ | |
use types::errors::Error::*; | |
let error = match self { | |
&GenericError | |
=> format!("Unknown Error"), | |
&DatabaseConnectionFailed(ref conn) | |
=> format!("Database connection failed: {}", conn ), | |
} | |
} | |
} | |
fn could_fail() -> Result<(), Error> { | |
try_res_err!(extremely_dangerous(), Error::GenericError); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment