use anyhow to add context to File::open errors
This commit is contained in:
parent
304ae6740d
commit
407fb26e50
5 changed files with 26 additions and 3 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -84,6 +84,12 @@ dependencies = [
|
||||||
"windows-sys",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.72"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "atom_syndication"
|
name = "atom_syndication"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
|
@ -1039,6 +1045,7 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
|
||||||
name = "podal"
|
name = "podal"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"atom_syndication",
|
"atom_syndication",
|
||||||
"bytes",
|
"bytes",
|
||||||
"clap",
|
"clap",
|
||||||
|
|
|
@ -11,6 +11,7 @@ reqwest = { version = "0.11.18", features = ["json", "blocking"] }
|
||||||
scraper = "0.17.1"
|
scraper = "0.17.1"
|
||||||
clap = {version = "4.3.19", features = ["derive"]}
|
clap = {version = "4.3.19", features = ["derive"]}
|
||||||
bytes = "1.4.0"
|
bytes = "1.4.0"
|
||||||
|
anyhow = "1.0.72"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempfile = "*"
|
tempfile = "*"
|
||||||
|
|
|
@ -13,6 +13,14 @@ impl Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl From<anyhow::Error> for Error {
|
||||||
|
fn from(value: anyhow::Error) -> Self {
|
||||||
|
Self {
|
||||||
|
details: value.to_string(),
|
||||||
|
source: value.source().unwrap().to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
impl From<Utf8Error> for Error {
|
impl From<Utf8Error> for Error {
|
||||||
fn from(value: Utf8Error) -> Self {
|
fn from(value: Utf8Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use anyhow::Context;
|
||||||
|
|
||||||
use crate::params::Args;
|
use crate::params::Args;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
@ -19,7 +21,9 @@ impl FileEnv {
|
||||||
Self {
|
Self {
|
||||||
open: Box::new(move |file_name| {
|
open: Box::new(move |file_name| {
|
||||||
let path = format!("{}/{}", &open_dir, file_name);
|
let path = format!("{}/{}", &open_dir, file_name);
|
||||||
let file = File::open(path)?;
|
let file = File::open(&path).with_context(|| {
|
||||||
|
format!("FileEnv::open: file_name={file_name}, path={path}")
|
||||||
|
})?;
|
||||||
Ok(file)
|
Ok(file)
|
||||||
}),
|
}),
|
||||||
append_line: Box::new(move |file_name, line| {
|
append_line: Box::new(move |file_name, line| {
|
||||||
|
|
|
@ -7,6 +7,7 @@ use std::{
|
||||||
sync::mpsc::Sender,
|
sync::mpsc::Sender,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
use tempfile::{tempdir, TempDir};
|
use tempfile::{tempdir, TempDir};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -63,10 +64,12 @@ pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
||||||
pub fn mock_file_open(real_paths: HashMap<String, String>) -> FileOpenFn {
|
pub fn mock_file_open(real_paths: HashMap<String, String>) -> FileOpenFn {
|
||||||
Box::new(move |path: &str| {
|
Box::new(move |path: &str| {
|
||||||
if let Some(real_path) = real_paths.get(&path.to_string()) {
|
if let Some(real_path) = real_paths.get(&path.to_string()) {
|
||||||
Ok(File::open(real_path)?)
|
Ok(File::open(real_path).with_context(|| {
|
||||||
|
format!("test_utils/mock_file_open: path={path}, real_path={real_path}, path_map=[{:?}]", real_paths)
|
||||||
|
})?)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::message(
|
Err(Error::message(
|
||||||
format!("Not implemented: file_open: {}", path).as_str(),
|
format!("Not implemented: test_utils/mock_file_open: {}", path).as_str(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue