refactor error handling with thiserror in dtpm module

This commit is contained in:
Noor 2025-02-18 13:59:14 +05:30
parent 987bbfd8ec
commit e835424049
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
3 changed files with 46 additions and 26 deletions

53
Cargo.lock generated

@ -176,7 +176,8 @@ dependencies = [
"base64", "base64",
"prost", "prost",
"serde", "serde",
"serde_yml", "serde_yaml",
"thiserror",
"tonic", "tonic",
"tonic-build", "tonic-build",
] ]
@ -454,16 +455,6 @@ version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "libyml"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3302702afa434ffa30847a83305f0a69d6abd74293b6554c18ec85c7ef30c980"
dependencies = [
"anyhow",
"version_check",
]
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.14" version = "0.4.14"
@ -758,9 +749,9 @@ checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.18" version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd"
[[package]] [[package]]
name = "serde" name = "serde"
@ -783,18 +774,16 @@ dependencies = [
] ]
[[package]] [[package]]
name = "serde_yml" name = "serde_yaml"
version = "0.0.12" version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59e2dd588bf1597a252c3b920e0143eb99b0f76e4e082f4c92ce34fbc9e71ddd" checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [ dependencies = [
"indexmap 2.7.0", "indexmap 2.7.0",
"itoa", "itoa",
"libyml",
"memchr",
"ryu", "ryu",
"serde", "serde",
"version_check", "unsafe-libyaml",
] ]
[[package]] [[package]]
@ -852,6 +841,26 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "thiserror"
version = "2.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.42.0" version = "1.42.0"
@ -1037,10 +1046,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]] [[package]]
name = "version_check" name = "unsafe-libyaml"
version = "0.9.5" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]] [[package]]
name = "want" name = "want"

@ -7,7 +7,8 @@ edition = "2021"
base64 = "0.22.1" base64 = "0.22.1"
prost = "0.13.4" prost = "0.13.4"
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.216", features = ["derive"] }
serde_yml = "0.0.12" serde_yaml = "0.9.34"
thiserror = "2.0.11"
tonic = "0.12.3" tonic = "0.12.3"
[build-dependencies] [build-dependencies]

@ -180,13 +180,23 @@ impl From<RestartPolicy> for pb_dtpm::RestartPolicy {
} }
} }
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Disk Access Error: {0}")]
DiskAccess(#[from] std::io::Error),
#[error("Yaml Error: {0}")]
SerdeYaml(#[from] serde_yaml::Error),
}
type Result<T> = std::result::Result<T, Error>;
impl DtpmConfig { impl DtpmConfig {
pub fn from_path(path: &str) -> Result<Self, Box<dyn std::error::Error>> { pub fn from_path(path: &str) -> Result<Self> {
let config_str = std::fs::read_to_string(path)?; let config_str = std::fs::read_to_string(path)?;
Ok(serde_yml::from_str(&config_str)?) Ok(serde_yaml::from_str(&config_str)?)
} }
pub fn load_data(mut self) -> Result<Self, Box<dyn std::error::Error>> { pub fn load_data(mut self) -> Result<Self> {
self.filesystems.iter_mut().for_each(|x| { self.filesystems.iter_mut().for_each(|x| {
if let FileContent::Path(path) = &x.content { if let FileContent::Path(path) = &x.content {
let content = let content =