diff options
author | Toby Vincent <tobyv13@gmail.com> | 2022-02-13 21:14:33 -0600 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2022-02-13 21:14:33 -0600 |
commit | 8985b2ffd7c8d6e9cf726f630f39ce6e8f00df79 (patch) | |
tree | 3e632e691378e36d389393d80049fa81c1b8f9cf /zone_core | |
parent | 96e571eca2991a19fc9bcdc26e451c4fad5bc791 (diff) |
refactor: add error handling to libraries
refactor libraries from using anyhow to explicitly handling errors
Diffstat (limited to 'zone_core')
-rw-r--r-- | zone_core/Cargo.toml | 2 | ||||
-rw-r--r-- | zone_core/src/lib.rs | 39 |
2 files changed, 28 insertions, 13 deletions
diff --git a/zone_core/Cargo.toml b/zone_core/Cargo.toml index 6051564..bd2a8f4 100644 --- a/zone_core/Cargo.toml +++ b/zone_core/Cargo.toml @@ -21,4 +21,4 @@ strum_macros = "0.23.1" tabled = "0.4.2" zone_zfs = { version = "0.1.0", path = "../zone_zfs" } zone_nspawn = { version = "0.1.0", path = "../zone_nspawn" } -anyhow = "1.0.53" +thiserror = "1.0.30" diff --git a/zone_core/src/lib.rs b/zone_core/src/lib.rs index 1c7fab2..d451940 100644 --- a/zone_core/src/lib.rs +++ b/zone_core/src/lib.rs @@ -1,10 +1,12 @@ -use std::path::PathBuf; - -use anyhow::{Context}; use clap::Args; -use rocket::{FromForm, FromFormField}; +use rocket::{ + response::{self, Responder}, + serde::json::Json, + FromForm, FromFormField, Request, +}; use rocket_okapi::okapi::schemars::{self, JsonSchema}; use serde::{Deserialize, Serialize}; +use std::path::PathBuf; use strum_macros::{Display, EnumString}; use tabled::Tabled; use zone_zfs::file_system::FileSystem; @@ -66,25 +68,38 @@ impl PartialEqOrDefault for Container { } } +#[rocket::async_trait] +impl<'r> Responder<'r, 'static> for Container { + fn respond_to(self, request: &'r Request<'_>) -> response::Result<'static> { + Json(self).respond_to(request) + } +} + impl TryFrom<FileSystem> for Container { - type Error = anyhow::Error; + type Error = zone_zfs::Error; fn try_from(file_system: FileSystem) -> Result<Self, Self::Error> { let path_buf = PathBuf::from(&file_system) .file_name() - .context(format!("Invalid FileSystem path: {:?}", file_system))? + .ok_or_else(|| { + Self::Error::FileSystem(format!("Invalid FileSystem path: {:?}", file_system)) + })? .to_string_lossy() .into_owned(); - let (user, id) = path_buf - .rsplit_once("-") - .context(format!("Invalid FileSystem name: {:?}", file_system))?; + let (user, id) = path_buf.rsplit_once("-").ok_or_else(|| { + Self::Error::FileSystem(format!("Invalid FileSystem name: {:?}", file_system)) + })?; - let id = id.parse::<u64>().context("Failed to parse container ID")?; + let id = id.parse::<u64>().map_err(|err| { + Self::Error::FileSystem(format!("Failed to parse container ID: {:?}", err)) + })?; let template = PathBuf::from(&file_system) .parent() - .context(format!("Invalid path for filesystem: {:?}", &file_system))? + .ok_or_else(|| { + Self::Error::FileSystem(format!("Invalid path for filesystem: {:?}", &file_system)) + })? .to_string_lossy() .into_owned(); @@ -98,7 +113,7 @@ impl TryFrom<FileSystem> for Container { } impl TryFrom<zone_nspawn::Container> for Container { - type Error = anyhow::Error; + type Error = zone_nspawn::Error; fn try_from(_value: zone_nspawn::Container) -> Result<Self, Self::Error> { todo!() |