Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split up FoundationError into method-specific error types #3385

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/dfx-core/src/config/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::error::config::ConfigError::{
DetermineConfigDirectoryFailed, DetermineSharedNetworkDirectoryFailed,
EnsureConfigDirectoryExistsFailed,
};
use crate::error::foundation::FoundationError;
use crate::error::foundation::FoundationError::NoHomeInEnvironment;
use crate::error::get_user_home::GetUserHomeError;
use crate::error::get_user_home::GetUserHomeError::NoHomeInEnvironment;
#[cfg(not(windows))]
use crate::foundation::get_user_home;
use crate::fs::composite::ensure_dir_exists;
use directories_next::ProjectDirs;
use std::path::PathBuf;

pub fn project_dirs() -> Result<&'static ProjectDirs, FoundationError> {
pub fn project_dirs() -> Result<&'static ProjectDirs, GetUserHomeError> {
lazy_static::lazy_static! {
static ref DIRS: Option<ProjectDirs> = ProjectDirs::from("org", "dfinity", "dfx");
}
Expand Down
7 changes: 6 additions & 1 deletion src/dfx-core/src/error/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ use super::{
archive::ArchiveError, fs::FsError, structured_file::StructuredFileError,
unified_io::UnifiedIoError,
};
use crate::error::get_current_exe::GetCurrentExeError;
use crate::error::get_user_home::GetUserHomeError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum CacheError {
#[error(transparent)]
FoundationError(#[from] crate::error::foundation::FoundationError),
GetCurrentExeError(#[from] GetCurrentExeError),

#[error(transparent)]
GetUserHomeError(#[from] GetUserHomeError),

#[error(transparent)]
UnifiedIo(#[from] crate::error::unified_io::UnifiedIoError),
Expand Down
6 changes: 3 additions & 3 deletions src/dfx-core/src/error/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::foundation::FoundationError;
use crate::error::fs::FsError;
use crate::error::get_user_home::GetUserHomeError;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -8,8 +8,8 @@ pub enum ConfigError {
EnsureConfigDirectoryExistsFailed(FsError),

#[error("Failed to determine config directory path: {0}")]
DetermineConfigDirectoryFailed(FoundationError),
DetermineConfigDirectoryFailed(GetUserHomeError),

#[error("Failed to determine shared network data directory: {0}")]
DetermineSharedNetworkDirectoryFailed(FoundationError),
DetermineSharedNetworkDirectoryFailed(GetUserHomeError),
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum FoundationError {
#[error("Cannot find home directory (no HOME environment variable).")]
NoHomeInEnvironment(),

pub enum GetCurrentExeError {
#[error("Failed to identify currently running executable: {0}")]
NoCurrentExe(std::io::Error),
}
7 changes: 7 additions & 0 deletions src/dfx-core/src/error/get_user_home.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum GetUserHomeError {
#[error("Cannot find home directory (no HOME environment variable).")]
NoHomeInEnvironment(),
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::error::foundation::FoundationError;
use crate::error::get_user_home::GetUserHomeError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum GetLegacyCredentialsPemPathError {
#[error("Failed to get legacy pem path: {0}")]
GetLegacyPemPathFailed(FoundationError),
GetLegacyPemPathFailed(GetUserHomeError),
}
3 changes: 2 additions & 1 deletion src/dfx-core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ pub mod config;
pub mod dfx_config;
pub mod encryption;
pub mod extension;
pub mod foundation;
pub mod fs;
pub mod get_current_exe;
pub mod get_user_home;
pub mod identity;
pub mod keyring;
pub mod load_dfx_config;
Expand Down
12 changes: 7 additions & 5 deletions src/dfx-core/src/foundation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::error::foundation::FoundationError;
use crate::error::foundation::FoundationError::NoHomeInEnvironment;
use crate::error::get_current_exe::GetCurrentExeError;
use crate::error::get_current_exe::GetCurrentExeError::NoCurrentExe;
use crate::error::get_user_home::GetUserHomeError;
use crate::error::get_user_home::GetUserHomeError::NoHomeInEnvironment;
use std::ffi::OsString;
use std::path::PathBuf;

pub fn get_user_home() -> Result<OsString, FoundationError> {
pub fn get_user_home() -> Result<OsString, GetUserHomeError> {
std::env::var_os("HOME").ok_or(NoHomeInEnvironment())
}

pub fn get_current_exe() -> Result<PathBuf, FoundationError> {
std::env::current_exe().map_err(FoundationError::NoCurrentExe)
pub fn get_current_exe() -> Result<PathBuf, GetCurrentExeError> {
std::env::current_exe().map_err(NoCurrentExe)
}
Loading