From 8d6bd4fd637be2c50403062f6f7e462b36647687 Mon Sep 17 00:00:00 2001 From: Maspenguin Date: Sun, 13 Dec 2020 23:19:49 +1100 Subject: [PATCH] Input: Json parsing errors now contain the parsed text (#31) --- src/input/error.rs | 13 ++++++------- src/input/ffmpeg_src.rs | 7 ++++++- src/input/ytdl_src.rs | 35 +++++++++++++++++------------------ 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/input/error.rs b/src/input/error.rs index ec8e68983..00ae0fcd4 100644 --- a/src/input/error.rs +++ b/src/input/error.rs @@ -16,7 +16,12 @@ pub enum Error { /// An error occurred while reading, or opening a file. Io(IoError), /// An error occurred while parsing JSON (i.e., during metadata/stereo detection). - Json(JsonError), + Json { + /// Json error + error: JsonError, + /// Text that failed to be parsed + parsed_text: String, + }, /// An error occurred within the Opus codec. Opus(OpusError), /// Failed to extract metadata from alternate pipe. @@ -57,12 +62,6 @@ impl From for Error { } } -impl From for Error { - fn from(e: JsonError) -> Self { - Error::Json(e) - } -} - impl From for Error { fn from(e: OpusError) -> Error { Error::Opus(e) diff --git a/src/input/ffmpeg_src.rs b/src/input/ffmpeg_src.rs index d094fb8aa..928d21033 100644 --- a/src/input/ffmpeg_src.rs +++ b/src/input/ffmpeg_src.rs @@ -140,7 +140,12 @@ pub(crate) async fn is_stereo(path: &OsStr) -> Result<(bool, Metadata)> { .output() .await?; - let value: Value = serde_json::from_reader(&out.stdout[..])?; + let value: Value = serde_json::from_reader(&out.stdout[..]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&out.stdout[..]) + .unwrap_or_default() + .to_string(), + })?; let metadata = Metadata::from_ffprobe_json(&value); diff --git a/src/input/ytdl_src.rs b/src/input/ytdl_src.rs index 19e1fcd4d..36619825c 100644 --- a/src/input/ytdl_src.rs +++ b/src/input/ytdl_src.rs @@ -67,29 +67,28 @@ pub(crate) async fn _ytdl(uri: &str, pre_args: &[&str]) -> Result { .spawn()?; let stderr = youtube_dl.stderr.take(); - let (returned_stderr, value) = task::spawn_blocking(move || { - if let Some(mut s) = stderr { - let out: Option = { - let mut o_vec = vec![]; - let mut serde_read = BufReader::new(s.by_ref()); - // Newline... - if let Ok(len) = serde_read.read_until(0xA, &mut o_vec) { - serde_json::from_slice(&o_vec[..len]).ok() - } else { - None - } - }; + let mut s = stderr.unwrap(); + let out: Result = { + let mut o_vec = vec![]; + let mut serde_read = BufReader::new(s.by_ref()); + // Newline... + if let Ok(len) = serde_read.read_until(0xA, &mut o_vec) { + serde_json::from_slice(&o_vec[..len]).map_err(|err| Error::Json { + error: err, + parsed_text: std::str::from_utf8(&o_vec).unwrap_or_default().to_string(), + }) + } else { + Result::Err(Error::Metadata) + } + }; - (Some(s), out) - } else { - (None, None) - } + (s, out) }) .await .map_err(|_| Error::Metadata)?; - youtube_dl.stderr = returned_stderr; + youtube_dl.stderr = Some(returned_stderr); let ffmpeg = Command::new("ffmpeg") .args(pre_args) @@ -101,7 +100,7 @@ pub(crate) async fn _ytdl(uri: &str, pre_args: &[&str]) -> Result { .stdout(Stdio::piped()) .spawn()?; - let metadata = Metadata::from_ytdl_output(value.unwrap_or_default()); + let metadata = Metadata::from_ytdl_output(value?); trace!("ytdl metadata {:?}", metadata);