Skip to content

Commit

Permalink
Input: Json parsing errors now contain the parsed text (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maspenguin authored Dec 13, 2020
1 parent 700f20d commit 8d6bd4f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
13 changes: 6 additions & 7 deletions src/input/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -57,12 +62,6 @@ impl From<IoError> for Error {
}
}

impl From<JsonError> for Error {
fn from(e: JsonError) -> Self {
Error::Json(e)
}
}

impl From<OpusError> for Error {
fn from(e: OpusError) -> Error {
Error::Opus(e)
Expand Down
7 changes: 6 additions & 1 deletion src/input/ffmpeg_src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
35 changes: 17 additions & 18 deletions src/input/ytdl_src.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,28 @@ pub(crate) async fn _ytdl(uri: &str, pre_args: &[&str]) -> Result<Input> {
.spawn()?;

let stderr = youtube_dl.stderr.take();

let (returned_stderr, value) = task::spawn_blocking(move || {
if let Some(mut s) = stderr {
let out: Option<Value> = {
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<Value> = {
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)
Expand All @@ -101,7 +100,7 @@ pub(crate) async fn _ytdl(uri: &str, pre_args: &[&str]) -> Result<Input> {
.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);

Expand Down

0 comments on commit 8d6bd4f

Please sign in to comment.