From 781c6b55ecc8174ddda9017516b69db4c1c7f10d Mon Sep 17 00:00:00 2001 From: einisto <79069176+einisto@users.noreply.github.com> Date: Wed, 15 Jun 2022 12:19:49 +0300 Subject: [PATCH] Handled reqwest & serde_json errors --- src/downloader.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/downloader.rs b/src/downloader.rs index cb31362..485341d 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -2,7 +2,7 @@ use colored::Colorize; use futures::{stream, StreamExt}; use reqwest::Client; use serde_json::Value; -use std::path::PathBuf; +use std::{path::PathBuf, process::exit}; use tokio::{fs::File, io::AsyncWriteExt}; type Result = std::result::Result>; @@ -12,8 +12,26 @@ pub async fn get_imagelist( board_name: &str, output_path: &PathBuf, ) -> Result> { - let req_body = reqwest::get(json_url).await?.text().await?; - let json_data: Value = serde_json::from_str(req_body.as_str())?; + let req_body_raw = match reqwest::get(json_url).await { + Ok(n) => n, + Err(_) => { + eprintln!("{}", format!("Error requesting {}", json_url).bold().red()); + exit(0x0100); + } + }; + let req_body_text = req_body_raw.text().await?; + let json_data: Value = match serde_json::from_str(req_body_text.as_str()) { + Ok(n) => n, + Err(e) => { + eprintln!( + "{}", + format!("Error parsing json from {}: {}", json_url, e) + .bold() + .red() + ); + exit(0x0100); + } + }; let mut img_data: Vec<(String, PathBuf)> = Vec::new(); json_data["posts"] @@ -57,10 +75,12 @@ pub async fn get_images(img_data: &Vec<(String, PathBuf)>) -> Result { } Err(_) => eprintln!( "{}", - format!("Error reading bytes from {}", url).bold().red() + format!("Error converting request from {} to bytes", url) + .bold() + .red() ), }, - Err(_) => eprintln!("{}", format!("Error downloading {}", url).bold().red()), + Err(_) => eprintln!("{}", format!("Error requesting {}", url).bold().red()), } })) .buffer_unordered(100)