-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use custom error types in internal and external crates (#182)
* feat: use custom error types in internal and external crates * chore: be explicit in tests about the expected and returned value * Apply suggestions from code review --------- Co-authored-by: Ángel M <[email protected]>
- Loading branch information
1 parent
854a621
commit 8b8c333
Showing
35 changed files
with
502 additions
and
127 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2023 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pub type Result<T> = std::result::Result<T, ConfigError>; | ||
|
||
#[derive(Debug)] | ||
pub enum ConfigError { | ||
CannotLoadConfig(std::io::Error), | ||
CannotParseConfig(toml::de::Error), | ||
CannotSaveConfig, | ||
} | ||
|
||
impl std::fmt::Display for ConfigError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::CannotLoadConfig(err) => write!(f, "Could not load configuration: {}", err), | ||
Self::CannotParseConfig(err) => write!(f, "Could not parse configuration: {}", err), | ||
Self::CannotSaveConfig => write!(f, "Could not save configuration"), | ||
} | ||
} | ||
} | ||
|
||
impl From<std::io::Error> for ConfigError { | ||
fn from(error: std::io::Error) -> Self { | ||
ConfigError::CannotLoadConfig(error) | ||
} | ||
} | ||
|
||
impl From<toml::de::Error> for ConfigError { | ||
fn from(error: toml::de::Error) -> ConfigError { | ||
ConfigError::CannotParseConfig(error) | ||
} | ||
} | ||
|
||
impl From<toml::ser::Error> for ConfigError { | ||
fn from(_: toml::ser::Error) -> ConfigError { | ||
ConfigError::CannotSaveConfig | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2023 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use wws_store::errors::StoreError; | ||
|
||
pub type Result<T> = std::result::Result<T, FetchError>; | ||
|
||
#[derive(Debug)] | ||
pub enum FetchError { | ||
DefaultBranchMissing, | ||
GitError(git2::Error), | ||
HttpError(reqwest::Error), | ||
InvalidURL, | ||
InvalidReusedRepository, | ||
InvalidRepository, | ||
InvalidChecksum, | ||
MissingPathInFilesystem, | ||
StoreError(StoreError), | ||
} | ||
|
||
impl std::fmt::Display for FetchError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::DefaultBranchMissing => write!( | ||
f, | ||
"Couldn't find the default main branch. Please, set the Git branch you want to use" | ||
), | ||
Self::GitError(err) => write!(f, "Git error: {}", err), | ||
Self::HttpError(err) => write!(f, "HTTP error: {}", err), | ||
Self::InvalidURL => write!(f, "Invalid URL"), | ||
Self::InvalidReusedRepository => write!(f, "Invalid local copy of repository"), | ||
Self::InvalidRepository => write!(f, "Invalid repository"), | ||
Self::InvalidChecksum => write!(f, "Invalid checksum"), | ||
Self::MissingPathInFilesystem => write!(f, "Missing path in filesystem"), | ||
Self::StoreError(err) => write!(f, "Store error: {}", err), | ||
} | ||
} | ||
} | ||
|
||
impl From<git2::Error> for FetchError { | ||
fn from(error: git2::Error) -> Self { | ||
FetchError::GitError(error) | ||
} | ||
} | ||
|
||
impl From<url::ParseError> for FetchError { | ||
fn from(_error: url::ParseError) -> Self { | ||
FetchError::InvalidURL | ||
} | ||
} | ||
|
||
impl From<reqwest::Error> for FetchError { | ||
fn from(error: reqwest::Error) -> Self { | ||
FetchError::HttpError(error) | ||
} | ||
} | ||
|
||
impl From<StoreError> for FetchError { | ||
fn from(error: StoreError) -> Self { | ||
FetchError::StoreError(error) | ||
} | ||
} | ||
|
||
impl From<std::string::FromUtf8Error> for FetchError { | ||
fn from(_error: std::string::FromUtf8Error) -> Self { | ||
FetchError::InvalidRepository | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.