Skip to content

Commit

Permalink
rexiftool: Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mindeng committed Sep 17, 2024
1 parent de008a4 commit 89ad402
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ GpsIso6709 => +22.5797+113.9380+028.396/
File: "testdata/compatible-brands-fail.heic"
------------------------------------------------
Error: unrecognized file format, please give feedback to the author on github
Unrecognized file format, consider filing a bug @ https://github.com/mindeng/nom-exif.
File: "testdata/webm_480.webm"
------------------------------------------------
Expand Down
46 changes: 27 additions & 19 deletions examples/rexiftool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn run(cli: &Cli) -> Result<(), Box<dyn Error>> {

let path = Path::new(&cli.file);
if path.is_file() {
parse_file(&mut parser, path, cli.json)?;
let _ = parse_file(&mut parser, path, cli.json);
} else if path.is_dir() {
parse_dir(path, parser, cli)?;
}
Expand All @@ -85,12 +85,7 @@ fn parse_dir(path: &Path, mut parser: MediaParser, cli: &Cli) -> Result<(), Box<
}
println!("File: {:?}", entry.path().as_os_str());
println!("------------------------------------------------");
match parse_file(&mut parser, entry.path(), cli.json) {
Ok(_) => (),
Err(e) => {
eprintln!("Error: {e}");
}
}
let _ = parse_file(&mut parser, entry.path(), cli.json);
}
Err(e) => {
eprintln!("Read dir entry failed: {e}");
Expand All @@ -105,10 +100,10 @@ fn parse_file<P: AsRef<Path>>(
parser: &mut MediaParser,
path: P,
json: bool,
) -> Result<(), Box<dyn Error>> {
let ms = MediaSource::file_path(path)?;
) -> Result<(), nom_exif::Error> {
let ms = MediaSource::file_path(path).inspect_err(handle_parsing_error)?;
let values = if ms.has_exif() {
let iter: ExifIter = parser.parse(ms)?;
let iter: ExifIter = parser.parse(ms).inspect_err(handle_parsing_error)?;
iter.into_iter()
.filter_map(|mut x| {
let res = x.take_result();
Expand Down Expand Up @@ -137,15 +132,17 @@ fn parse_file<P: AsRef<Path>>(
use std::collections::HashMap;

#[cfg(feature = "json_dump")]
println!(
"{}",
serde_json::to_string_pretty(
&values
.into_iter()
.map(|x| (x.0.to_string(), x.1))
.collect::<HashMap<_, _>>()
)?
);
match serde_json::to_string_pretty(
&values
.into_iter()
.map(|x| (x.0.to_string(), x.1))
.collect::<HashMap<_, _>>(),
) {
Ok(s) => {
println!("{}", s);
}
Err(e) => eprintln!("Error: {e}"),
}
} else {
values.iter().for_each(|x| {
println!("{:<32}=> {}", x.0, x.1);
Expand All @@ -154,6 +151,17 @@ fn parse_file<P: AsRef<Path>>(
Ok(())
}

fn handle_parsing_error(e: &nom_exif::Error) {
match e {
nom_exif::Error::UnrecognizedFileFormat => {
eprintln!("Unrecognized file format, consider filing a bug @ https://github.com/mindeng/nom-exif.");
}
nom_exif::Error::ParseFailed(_) | nom_exif::Error::IOError(_) => {
eprintln!("Error: {e}");
}
}
}

fn init_tracing() -> io::Result<()> {
let stdout_log = tracing_subscriber::fmt::layer().pretty();
let subscriber = Registry::default().with(stdout_log);
Expand Down
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub enum Error {
#[error("io error: {0}")]
IOError(std::io::Error),

#[error("unrecognized file format, please give feedback to the author on github")]
/// If you encounter this error, please consider filing a bug on github
#[error("unrecognized file format")]
UnrecognizedFileFormat,
}

Expand Down

0 comments on commit 89ad402

Please sign in to comment.