Skip to content

Commit

Permalink
refactor(errors)!: Improve error handling, display and clean up codeb…
Browse files Browse the repository at this point in the history
…ase (#321)

- Grouped imports for better organization: `ErrorKind`, `RusticError`, `RusticResult`
- Fixed and updated tests for new error handling logic
- Addressed Clippy lints and improved code quality
- Ensured build compatibility for *nix and OSX platforms
- Updated function documentation to reflect new error handling
- Removed unused local error variants after refactor
- Verified `rustic-rs` functionality against the updated branch
  • Loading branch information
simonsan authored Nov 16, 2024
1 parent cd26d87 commit 40ad287
Show file tree
Hide file tree
Showing 68 changed files with 4,448 additions and 2,905 deletions.
135 changes: 87 additions & 48 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ resolver = "2"
rust-version = "1.76.0"

[workspace.dependencies]
# Internal Dependencies
rustic_backend = { path = "crates/backend", version = "0" }
rustic_core = { path = "crates/core", version = "0" }
rustic_testing = { path = "crates/testing", version = "0" }

aho-corasick = "1.1.3"
anyhow = "1.0.89"
bytes = "1.7.2"
displaydoc = "0.2.5"
enum-map = "2.7.3"
rustic_backend = { path = "crates/backend" }
rustic_core = { path = "crates/core", version = "0" }
rustic_testing = { path = "crates/testing", version = "0" }
log = "0.4.22"
simplelog = "0.12.2"
thiserror = "2.0.0"

# dev-dependencies
rstest = "0.23.0"
Expand Down
10 changes: 5 additions & 5 deletions crates/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ rclone = ["rest", "dep:rand", "dep:semver"]
rustic_core = { workspace = true }

# errors
anyhow = "1.0.89"
displaydoc = "0.2.5"
thiserror = "1.0.64"
displaydoc = { workspace = true }
thiserror = { workspace = true }

# logging
log = "0.4.22"
log = { workspace = true }

# other dependencies
bytes = "1.7.2"
bytes = { workspace = true }
derive_setters = "0.1.6"
humantime = "2.1.0"
itertools = "0.13.0"
Expand Down Expand Up @@ -101,6 +100,7 @@ opendal = { version = "0.50.0", features = ["services-b2", "services-sftp", "ser
opendal = { version = "0.50.0", features = ["services-b2", "services-swift", "services-azblob", "services-azdls", "services-cos", "services-fs", "services-ftp", "services-dropbox", "services-gdrive", "services-gcs", "services-ghac", "services-http", "services-ipmfs", "services-memory", "services-obs", "services-onedrive", "services-oss", "services-s3", "services-webdav", "services-webhdfs", "services-azfile", "layers-blocking", "layers-throttle"], optional = true }

[dev-dependencies]
anyhow = { workspace = true }
rstest = { workspace = true }
toml = "0.8.19"

Expand Down
37 changes: 21 additions & 16 deletions crates/backend/src/choose.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
//! This module contains [`BackendOptions`] and helpers to choose a backend from a given url.
use anyhow::{anyhow, Result};
use derive_setters::Setters;
use std::{collections::HashMap, sync::Arc};
use strum_macros::{Display, EnumString};

#[allow(unused_imports)]
use rustic_core::{RepositoryBackends, WriteBackend};
use rustic_core::{ErrorKind, RepositoryBackends, RusticError, RusticResult, WriteBackend};

use crate::{
error::BackendAccessErrorKind,
local::LocalBackend,
util::{location_to_type_and_path, BackendLocation},
};
Expand Down Expand Up @@ -75,12 +72,17 @@ impl BackendOptions {
/// # Returns
///
/// The backends for the repository.
pub fn to_backends(&self) -> Result<RepositoryBackends> {
pub fn to_backends(&self) -> RusticResult<RepositoryBackends> {
let mut options = self.options.clone();
options.extend(self.options_cold.clone());
let be = self
.get_backend(self.repository.as_ref(), options)?
.ok_or_else(|| anyhow!("No repository given."))?;
.ok_or_else(|| {
RusticError::new(
ErrorKind::Backend,
"No repository given. Please make sure, that you have set the repository.",
)
})?;
let mut options = self.options.clone();
options.extend(self.options_hot.clone());
let be_hot = self.get_backend(self.repo_hot.as_ref(), options)?;
Expand All @@ -97,7 +99,7 @@ impl BackendOptions {
///
/// # Errors
///
/// If the backend cannot be loaded, an error is returned.
/// * If the backend cannot be loaded, an error is returned.
///
/// # Returns
///
Expand All @@ -108,13 +110,18 @@ impl BackendOptions {
&self,
repo_string: Option<&String>,
options: HashMap<String, String>,
) -> Result<Option<Arc<dyn WriteBackend>>> {
) -> RusticResult<Option<Arc<dyn WriteBackend>>> {
repo_string
.map(|string| {
let (be_type, location) = location_to_type_and_path(string)?;
be_type.to_backend(location, options.into()).map_err(|err| {
BackendAccessErrorKind::BackendLoadError(be_type.to_string(), err).into()
})
be_type
.to_backend(location.clone(), options.into())
.map_err(|err| {
err
.prepend_guidance_line("Could not load the backend `{name}` at `{location}`. Please check the given backend and try again.")
.attach_context("name", be_type.to_string())
.attach_context("location", location.to_string())
})
})
.transpose()
}
Expand All @@ -131,14 +138,12 @@ pub trait BackendChoice {
///
/// # Errors
///
/// * [`BackendAccessErrorKind::BackendNotSupported`] - If the backend is not supported.
///
/// [`BackendAccessErrorKind::BackendNotSupported`]: crate::error::BackendAccessErrorKind::BackendNotSupported
/// * If the backend is not supported.
fn to_backend(
&self,
location: BackendLocation,
options: Option<HashMap<String, String>>,
) -> Result<Arc<dyn WriteBackend>>;
) -> RusticResult<Arc<dyn WriteBackend>>;
}

/// The supported backend types.
Expand Down Expand Up @@ -176,7 +181,7 @@ impl BackendChoice for SupportedBackend {
&self,
location: BackendLocation,
options: Option<HashMap<String, String>>,
) -> Result<Arc<dyn WriteBackend>> {
) -> RusticResult<Arc<dyn WriteBackend>> {
let options = options.unwrap_or_default();

Ok(match self {
Expand Down
134 changes: 0 additions & 134 deletions crates/backend/src/error.rs

This file was deleted.

24 changes: 14 additions & 10 deletions crates/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,22 @@ This crate exposes a few features for controlling dependency usage:
*/

pub mod choose;
/// Error types for the backend.
pub mod error;
/// Local backend for Rustic.
pub mod local;
/// Utility functions for the backend.
pub mod util;

/// `OpenDAL` backend for Rustic.
#[cfg(feature = "opendal")]
pub mod opendal;

/// `Rclone` backend for Rustic.
#[cfg(feature = "rclone")]
pub mod rclone;

/// REST backend for Rustic.
#[cfg(feature = "rest")]
pub mod rest;
/// Utility functions for the backend.
pub mod util;

// rustic_backend Public API
pub use crate::{
choose::{BackendOptions, SupportedBackend},
local::LocalBackend,
};

#[cfg(feature = "opendal")]
pub use crate::opendal::OpenDALBackend;
Expand All @@ -83,3 +78,12 @@ pub use crate::rclone::RcloneBackend;

#[cfg(feature = "rest")]
pub use crate::rest::RestBackend;

// rustic_backend Public API
pub use crate::{
choose::{BackendOptions, SupportedBackend},
local::LocalBackend,
};

// re-export for error handling
pub use rustic_core::{ErrorKind, RusticError, RusticResult, Severity, Status};
Loading

0 comments on commit 40ad287

Please sign in to comment.