Skip to content

Commit 81ffa60

Browse files
committed
fix: show useful errors when import file parsing fails
1 parent eb00769 commit 81ffa60

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/importers/importer.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
use std::{error::Error, fmt::Debug, fs::read_to_string, path::PathBuf};
1+
use std::{fmt::Debug, fs::read_to_string, path::PathBuf};
22

3+
use color_eyre::eyre::{eyre, Result};
34
use serde::Deserialize;
45

56
use crate::otp::otp_element::OTPElement;
67

78
/// Common flow for all the importers
8-
pub fn import_from_path<T>(path: PathBuf) -> Result<Vec<OTPElement>, Box<dyn Error>>
9+
pub fn import_from_path<T>(path: PathBuf) -> Result<Vec<OTPElement>>
910
where
1011
T: for<'a> Deserialize<'a> + TryInto<Vec<OTPElement>>,
1112
<T as TryInto<Vec<OTPElement>>>::Error: Debug,
1213
{
1314
let json = read_to_string(path)?;
14-
let deserialized: T = serde_json::from_str(json.as_str()).map_err(|e| e.to_string())?;
15-
let mapped: Vec<OTPElement> = deserialized.try_into().map_err(|e| format!("{:?}", e))?;
15+
let deserialized: T = serde_json::from_str(json.as_str()).map_err(|e| {
16+
eyre!(
17+
"Invalid JSON import format.
18+
Please check the file you are trying to import. For further information please check these guidelines:
19+
https://github.com/replydev/cotp?tab=readme-ov-file#migration-from-other-apps
20+
21+
Specific error: {:?}",
22+
e
23+
)
24+
})?;
25+
let mapped: Vec<OTPElement> = deserialized.try_into().map_err(|e| eyre!("{:?}", e))?;
1626
Ok(mapped)
1727
}

0 commit comments

Comments
 (0)