Created
August 23, 2018 17:51
-
-
Save ChunMinChang/92d0006fb9fe35abcabff6983d31f0da to your computer and use it in GitHub Desktop.
Passing error from inner modules to outer modules
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
// Converting the error types of inner modules to the error types of outer modules. | |
mod server { | |
use std::fmt; // For fmt::Debug trait. | |
pub enum Error { | |
File(file::Error), | |
Network(network::Error), | |
Others, | |
} | |
// Self-defined error messages: | |
impl fmt::Debug for Error { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
let printable = match self { | |
Error::File(e) => format!("File error: {:?}", e), | |
Error::Network(e) => format!("Network error: {:?}", e), | |
_ => "Other errors".to_string(), | |
}; | |
write!(f, "{}", printable) | |
} | |
} | |
// To convert a network::Error to a Error. | |
impl From<file::Error> for Error { | |
fn from(e: file::Error) -> Error { | |
Error::File(e) | |
} | |
} | |
// To convert a network::Error to a Error. | |
impl From<network::Error> for Error { | |
fn from(e: network::Error) -> Error { | |
Error::Network(e) | |
} | |
} | |
mod file { | |
use std::fmt; | |
pub enum Error { | |
NoPermission, | |
NotFound, | |
} | |
impl fmt::Debug for Error { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
let printable = match self { | |
Error::NoPermission => "No permission to read/write files.", | |
Error::NotFound => "Cannot find the file.", | |
}; | |
write!(f, "{}", printable) | |
} | |
} | |
pub struct Config { | |
pub port: u32, | |
pub address: String, | |
} | |
pub fn read_config() -> Result<Config, Error> { | |
Ok(Config { | |
port: 255, | |
address: "255.255.255.0".to_string(), | |
}) | |
// Err(Error::NoPermission) | |
} | |
} | |
mod network { | |
#[derive(Debug)] | |
pub enum Error { | |
BadRequest, | |
PortIsOccupied, | |
} | |
pub fn connect(address: &str, port: u32) -> Result<(), Error> { | |
Err(Error::PortIsOccupied) | |
// Err(Error::BadRequest) | |
} | |
} | |
pub struct Connection { | |
pub port: u32, | |
pub address: String, | |
} | |
pub fn start() -> Result<Connection, Error> { | |
let config = file::read_config()?; | |
network::connect(&config.address, config.port)?; | |
Ok(Connection { | |
port: config.port, | |
address: config.address, | |
}) | |
// Err(Error::Others) | |
} | |
} | |
fn main() { | |
let connection = server::start().unwrap(); | |
println!("port: {}, address: {}", connection.port, connection.address); | |
} |
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
// Implmenting std::fmt::Debug trait on our own. | |
mod server { | |
use std::fmt; // For fmt::Debug trait. | |
pub enum Error { | |
File(file::Error), | |
Network(network::Error), | |
Others, | |
} | |
// Self-defined error messages: | |
impl fmt::Debug for Error { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
let printable = match self { | |
Error::File(e) => format!("File error: {:?}", e), | |
Error::Network(e) => format!("Network error: {:?}", e), | |
_ => "Other errors".to_string(), | |
}; | |
write!(f, "{}", printable) | |
} | |
} | |
mod file { | |
use std::fmt; | |
pub enum Error { | |
NoPermission, | |
NotFound, | |
} | |
impl fmt::Debug for Error { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
let printable = match self { | |
Error::NoPermission => "No permission to read/write files.", | |
Error::NotFound => "Cannot find the file.", | |
}; | |
write!(f, "{}", printable) | |
} | |
} | |
} | |
mod network { | |
#[derive(Debug)] | |
pub enum Error { | |
BadRequest, | |
PortIsOccupied, | |
} | |
} | |
pub struct Connection { | |
pub port: u32, | |
pub address: String, | |
} | |
pub fn start() -> Result<Connection, Error> { | |
Err(Error::File(file::Error::NoPermission)) // File error: No permission to read/write files. | |
// Err(Error::Network(network::Error::BadRequest)) // Network error: BadRequest | |
// Err(Error::Others) // Other errors | |
} | |
} | |
fn main() { | |
let connection = server::start().unwrap(); | |
println!("port: {}, address: {}", connection.port, connection.address); | |
} |
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
// Using #[derive(Debug)] for std::fmt::Debug trait. | |
mod server { | |
#[derive(Debug)] | |
pub enum Error { | |
File(file::Error), | |
Network(network::Error), | |
Others, | |
} | |
mod file { | |
#[derive(Debug)] | |
pub enum Error { | |
NoPermission, | |
NotFound, | |
} | |
} | |
mod network { | |
#[derive(Debug)] | |
pub enum Error { | |
BadRequest, | |
PortIsOccupied, | |
} | |
} | |
pub struct Connection { | |
pub port: u32, | |
pub address: String, | |
} | |
pub fn start() -> Result<Connection, Error> { | |
Err(Error::File(file::Error::NoPermission)) // File(NoPermission) | |
// Err(Error::Network(network::Error::BadRequest)) // Network(BadRequest) | |
// Err(Error::Others) // Others | |
} | |
} | |
fn main() { | |
let connection = server::start().unwrap(); | |
println!("port: {}, address: {}", connection.port, connection.address); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment