Last active
October 10, 2018 04:45
-
-
Save Jwata/52c957774a2998815ade918758d78291 to your computer and use it in GitHub Desktop.
Refactoring error handling in Rust
This file contains hidden or 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
| // main.rs | |
| mod request; | |
| use request::request; | |
| fn main() { | |
| match request() { | |
| Ok(text) => println!("{}", text), | |
| Err(err) => println!("{}", err) | |
| } | |
| } |
This file contains hidden or 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
| extern crate reqwest; | |
| use reqwest::StatusCode; | |
| pub fn request() -> Result<String, String> { | |
| match reqwest::get("https://www.rust-lang.org/en-US") { | |
| Ok(mut r) => match r.status() { | |
| StatusCode::OK => match r.text() { | |
| Ok(t) => return Ok(t), | |
| Err(_) => return Err("Failed to extract text from response".to_string()) | |
| }, | |
| s => return Err(format!("Invalid Status Code: {:?}", s)), | |
| }, | |
| Err(_) => return Err("Request Failed".to_string()), | |
| } | |
| } |
This file contains hidden or 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
| // main.rs | |
| mod request; | |
| mod error; | |
| use request::request; | |
| fn main() { | |
| match request() { | |
| Ok(text) => println!("{}", text), | |
| Err(err) => println!("{}", err), | |
| } | |
| } |
This file contains hidden or 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
| // request.rs | |
| extern crate reqwest; | |
| use error::Error; | |
| type RequestResult<T> = Result<T, Error>; | |
| pub fn request() -> RequestResult<String> { | |
| let mut res = | |
| reqwest::get("http://sample.com/")?; | |
| if !res.status().is_success() { | |
| let err = Error::InvalidStatusCode { code: res.status() }; | |
| return Err(err); | |
| } | |
| return Ok(res.text()?); | |
| } |
This file contains hidden or 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
| extern crate reqwest; | |
| use std::fmt; | |
| pub enum Error { | |
| HttpError(reqwest::Error), | |
| InvalidStatusCode { code: reqwest::StatusCode }, | |
| } | |
| impl fmt::Display for Error { | |
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| match *self { | |
| Error::HttpError(ref e) => writeln!(f, "Http Request Failed. {}", e), | |
| Error::InvalidStatusCode { code: c } => writeln!(f, "Invalid Status Code. {}", c), | |
| } | |
| } | |
| } | |
| impl From<reqwest::Error> for Error { | |
| fn from(e: reqwest::Error) -> Error { | |
| Error::HttpError(e) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
01_request.rsis ugly because of the conditional logics. It can be refactored by definingRequestResultas you see in11_request.rs, so you can useget.(...)?,text(...)?without the error handlings.