Skip to content

Commit 05d98f1

Browse files
committed
improve error handling/message on malformed JSON
1 parent 220b0c0 commit 05d98f1

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/count.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ async fn game_count_get(
167167
Ok(text) => match serde_json::from_str(&text) {
168168
Ok(text_json) => {
169169
let text_json: serde_json::Value = text_json;
170+
171+
let mut json_is_ok = false;
172+
match text_json.as_object() {
173+
Some(obj_json) => {
174+
if obj_json.contains_key("response") {
175+
match obj_json["response"].as_object() {
176+
Some(obj_response_json) => {
177+
if obj_response_json.contains_key("player_count") {
178+
json_is_ok = true;
179+
}
180+
}
181+
None => (),
182+
}
183+
}
184+
}
185+
None => (),
186+
}
187+
if !json_is_ok {
188+
eprintln!("ERROR: Malformed JSON response");
189+
return Ok(());
190+
}
191+
170192
let count = text_json["response"]["player_count"]
171193
.to_string()
172194
.parse::<u32>()

0 commit comments

Comments
 (0)