import: improve error messages (#287)
This commit is contained in:
parent
d31a1435c2
commit
9c6d10dc1f
2 changed files with 42 additions and 8 deletions
|
@ -168,19 +168,27 @@ impl AccountManager {
|
|||
.insert(account.account_name.clone(), Arc::new(Mutex::new(account)));
|
||||
}
|
||||
|
||||
pub fn import_account(&mut self, import_path: &String) -> anyhow::Result<()> {
|
||||
pub fn import_account(
|
||||
&mut self,
|
||||
import_path: &String,
|
||||
) -> anyhow::Result<(), ManifestAccountImportError> {
|
||||
let path = Path::new(import_path);
|
||||
ensure!(path.exists(), "{} does not exist.", import_path);
|
||||
ensure!(path.is_file(), "{} is not a file.", import_path);
|
||||
if !path.exists() {
|
||||
return Err(ManifestAccountImportError::FileNotFound);
|
||||
}
|
||||
if !path.is_file() {
|
||||
return Err(ManifestAccountImportError::NotAFile);
|
||||
}
|
||||
|
||||
let file = File::open(path)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut deser = serde_json::Deserializer::from_reader(reader);
|
||||
let account: SteamGuardAccount = serde_path_to_error::deserialize(&mut deser)?;
|
||||
ensure!(
|
||||
!self.account_exists(&account.account_name),
|
||||
"Account already exists in manifest, please remove it first."
|
||||
);
|
||||
if self.account_exists(&account.account_name) {
|
||||
return Err(ManifestAccountImportError::AlreadyExists {
|
||||
account_name: account.account_name,
|
||||
});
|
||||
}
|
||||
self.add_account(account);
|
||||
|
||||
Ok(())
|
||||
|
@ -438,6 +446,24 @@ impl From<std::io::Error> for ManifestAccountLoadError {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ManifestAccountImportError {
|
||||
#[error("Could not find the specified file.")]
|
||||
FileNotFound,
|
||||
#[error("The specified path is not a file.")]
|
||||
NotAFile,
|
||||
#[error(
|
||||
"The account you are trying to import, \"{account_name}\", already exists in the manifest."
|
||||
)]
|
||||
AlreadyExists { account_name: String },
|
||||
#[error(transparent)]
|
||||
IOError(#[from] std::io::Error),
|
||||
#[error("Failed to deserialize the account. {self:?}")]
|
||||
DeserializationFailed(#[from] serde_path_to_error::Error<serde_json::Error>),
|
||||
#[error(transparent)]
|
||||
Unknown(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::path::Path;
|
|||
|
||||
use log::*;
|
||||
|
||||
use crate::AccountManager;
|
||||
use crate::{accountmanager::ManifestAccountImportError, AccountManager};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
@ -24,6 +24,7 @@ where
|
|||
{
|
||||
fn execute(&self, _transport: T, manager: &mut AccountManager) -> anyhow::Result<()> {
|
||||
for file_path in self.files.iter() {
|
||||
debug!("loading entry: {:?}", file_path);
|
||||
if self.sda {
|
||||
let path = Path::new(&file_path);
|
||||
let account = crate::accountmanager::migrate::load_and_upgrade_sda_account(path)?;
|
||||
|
@ -34,6 +35,13 @@ where
|
|||
Ok(_) => {
|
||||
info!("Imported account: {}", &file_path);
|
||||
}
|
||||
Err(ManifestAccountImportError::AlreadyExists { .. }) => {
|
||||
warn!("Account already exists: {} -- Ignoring", &file_path);
|
||||
}
|
||||
Err(ManifestAccountImportError::DeserializationFailed(err)) => {
|
||||
warn!("Failed to import account: {} {}", &file_path, err);
|
||||
warn!("If this file came from SDA, try using --sda");
|
||||
}
|
||||
Err(err) => {
|
||||
bail!("Failed to import account: {} {}", &file_path, err);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue