diff options
author | NerdyPepper <[email protected]> | 2018-06-21 12:45:30 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2018-06-21 12:45:30 +0100 |
commit | 81379203747a64ab9d7954c06ffb3b565980299d (patch) | |
tree | 060ab737c51cc6a82232cb8199fc1057c909801d | |
parent | edde5085ebe7816f31d0e52ae71edf6f32bd1092 (diff) |
Handle errors the rust way, reduce panics
-rw-r--r-- | src/content.rs | 73 |
1 files changed, 36 insertions, 37 deletions
diff --git a/src/content.rs b/src/content.rs index 19293d2..ba1a460 100644 --- a/src/content.rs +++ b/src/content.rs | |||
@@ -49,7 +49,7 @@ pub fn get_extract(title: &str) -> Result<String, reqwest::Error> { | |||
49 | 49 | ||
50 | let mut v: Value = match serde_json::from_str(&res.text()?) { | 50 | let mut v: Value = match serde_json::from_str(&res.text()?) { |
51 | Ok(x) => x, | 51 | Ok(x) => x, |
52 | Err(x) => , | 52 | Err(x) => panic!("Failed to parse json\nReceived error {}", x), |
53 | }; | 53 | }; |
54 | let pageid = &v["query"]["pageids"][0]; | 54 | let pageid = &v["query"]["pageids"][0]; |
55 | let pageid_str = match pageid { | 55 | let pageid_str = match pageid { |
@@ -57,8 +57,16 @@ pub fn get_extract(title: &str) -> Result<String, reqwest::Error> { | |||
57 | _ => panic!("wut"), | 57 | _ => panic!("wut"), |
58 | }; | 58 | }; |
59 | 59 | ||
60 | Ok(format!("{}", &v["query"]["pages"][pageid_str]["extract"])) | 60 | match &v["query"]["pages"][pageid_str]["extract"] { |
61 | Value::String(extract) => { | ||
61 | 62 | ||
63 | // format to plain text | ||
64 | extract.replace("\\\\", "\\"); | ||
65 | Ok(format!("{}", extract)) | ||
66 | } | ||
67 | // ignore non strings | ||
68 | _ => Ok(format!("")) | ||
69 | } | ||
62 | } | 70 | } |
63 | 71 | ||
64 | pub fn get_title(title: &str, mut res: Response) -> String { | 72 | pub fn get_title(title: &str, mut res: Response) -> String { |
@@ -69,60 +77,51 @@ pub fn get_title(title: &str, mut res: Response) -> String { | |||
69 | format!("{}", &v["query"]["normalized"][0]["to"]) | 77 | format!("{}", &v["query"]["normalized"][0]["to"]) |
70 | } | 78 | } |
71 | 79 | ||
72 | pub fn get_search_results(search: &str) -> Vec<String> { | 80 | pub fn get_search_results(search: &str) -> Result<Vec<String>, reqwest::Error> { |
73 | 81 | ||
74 | let url = search_url_gen(search); | 82 | let url = search_url_gen(search); |
75 | let res = reqwest::get(&url[..]); | 83 | let res = reqwest::get(&url[..])?; |
76 | 84 | let mut v: Value = serde_json::from_str(&res.text().unwrap()) | |
77 | match res { | 85 | .unwrap_or_else( |e| { |
78 | Ok(mut res) => { | 86 | panic!("Recieved error {:?}", e); |
79 | if res.status().is_success() { | 87 | } ); |
80 | let mut v: Value = serde_json::from_str(&res.text().unwrap()) | 88 | |
81 | .unwrap_or_else( |e| { | 89 | let mut results: Vec<String> = vec![]; |
82 | panic!("Recieved error {:?}", e); | 90 | for item in v[1].as_array().unwrap() { |
83 | } ); | 91 | match item { |
84 | 92 | Value::String(x) => results.push(x.to_string()), | |
85 | let mut results: Vec<String> = vec![]; | 93 | // ignore non strings |
86 | for item in v[1].as_array().unwrap() { | 94 | _ => (), |
87 | match item { | ||
88 | Value::String(x) => results.push(x.to_string()), | ||
89 | _ => (), | ||
90 | } | ||
91 | } | ||
92 | results | ||
93 | } else { | ||
94 | panic!("Encountered Error {}", res.status()); | ||
95 | } | ||
96 | }, | ||
97 | _ => { | ||
98 | panic!("Unable to parse url"); | ||
99 | } | 95 | } |
100 | } | 96 | } |
101 | 97 | Ok(results) | |
102 | } | 98 | } |
103 | 99 | ||
104 | pub fn pop_error(s: &mut Cursive, msg: &str) { | 100 | pub fn pop_error(s: &mut Cursive, msg: String) { |
105 | s.add_layer(Dialog::text("An error occurred\n:(") | 101 | s.add_layer(Dialog::text(format!("{}", msg)) |
106 | .title("Oopsie woopsie") | 102 | .title("Error") |
107 | .button("Ok", |s| s.quit())); | 103 | .button("Ok", |s| s.quit())); |
108 | } | 104 | } |
109 | 105 | ||
110 | fn handler(e: reqwest::Error) -> &str { | 106 | pub fn handler(e: reqwest::Error) -> String { |
107 | let msg: String = String::new(); | ||
111 | if e.is_http() { | 108 | if e.is_http() { |
112 | match e.url() { | 109 | match e.url() { |
113 | None => format!("No URL given"), | 110 | None => msg = format!("No URL given"), |
114 | Some(url) => format!("Problem making request to: {}", url), | 111 | Some(url) => msg = format!("Problem making request to: {}", url), |
115 | } | 112 | } |
116 | } | 113 | } |
117 | // Inspect the internal error and output it | 114 | // Inspect the internal error and output it |
118 | if e.is_serialization() { | 115 | if e.is_serialization() { |
119 | let serde_error = match e.get_ref() { | 116 | let serde_error = match e.get_ref() { |
120 | None => return, | ||
121 | Some(err) => err, | 117 | Some(err) => err, |
122 | }; | 118 | }; |
123 | format!("problem parsing information {}", serde_error) | 119 | msg.push(format!("\nproblem parsing information {}", serde_error)); |
120 | } | ||
124 | 121 | ||
125 | if e.is_redirect() { | 122 | if e.is_redirect() { |
126 | format!("server redirecting too many times or making loop") | 123 | msg.push(format!("server redirecting too many times or making loop")); |
127 | } | 124 | } |
125 | |||
126 | msg | ||
128 | } | 127 | } |