Skip to content

Commit

Permalink
import: improve error messages (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 authored Jul 10, 2023
1 parent d31a143 commit 9c6d10d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
40 changes: 33 additions & 7 deletions src/accountmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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::*;
Expand Down
10 changes: 9 additions & 1 deletion src/commands/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::Path;

use log::*;

use crate::AccountManager;
use crate::{accountmanager::ManifestAccountImportError, AccountManager};

use super::*;

Expand All @@ -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)?;
Expand All @@ -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);
}
Expand Down

0 comments on commit 9c6d10d

Please sign in to comment.