-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: replace anyhow #18
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
9e3160b
7780614
a6c4080
5c32031
e519efa
e481e7e
346c275
cbb94fc
9d3f173
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| //TODO: No anyhow | ||
| use anyhow::Result; | ||
| use lum_config::MergeFrom; | ||
| use lum_libs::serde::{Deserialize, Serialize}; | ||
| use lum_log::{debug, error, info}; | ||
| use std::{fs, path::Path}; | ||
| use lum_log::{debug, info}; | ||
| use std::{fs, io, path::Path}; | ||
| use thiserror::Error; | ||
|
|
||
| use crate::{ | ||
| config::provider::Provider, | ||
|
|
@@ -14,6 +13,22 @@ pub mod dns; | |
| pub mod provider; | ||
| pub mod resolver; | ||
|
|
||
| /// Error type for configuration loading and parsing. | ||
| #[derive(Debug, Error)] | ||
| pub enum ConfigError { | ||
| #[error("IO error: {0}")] | ||
| Io(#[from] io::Error), | ||
|
|
||
| #[error("YAML parsing error: {0}")] | ||
| Yaml(#[from] serde_yaml_ng::Error), | ||
|
|
||
| #[error("Unknown provider config file: {0}")] | ||
| UnknownProvider(String), | ||
|
|
||
| #[error("Cannot determine DNS config type for file: {0}")] | ||
| UnknownDnsType(String), | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check if it makes sense to use individual Error enums for the different methods below instead of having them all return the same general ConfigError enum? Or does every method below actually need all of the enum values? Then using the same ConfigError enum for all of them makes sense. Otherwise, define specific error enums per method. |
||
| /// Configuration for the dnrs application. | ||
| /// | ||
| /// This struct holds all the configuration required to run the application, | ||
|
|
@@ -28,7 +43,7 @@ pub struct Config { | |
| } | ||
|
|
||
| impl Config { | ||
| pub fn load_from_directory(config_dir: impl AsRef<Path>) -> Result<Self> { | ||
| pub fn load_from_directory(config_dir: impl AsRef<Path>) -> Result<Self, ConfigError> { | ||
| let config_dir = config_dir.as_ref(); | ||
| let resolver = Self::load_resolver_config(config_dir)?; | ||
| let providers = Self::load_provider_configs(&config_dir.join("providers"))?; | ||
|
|
@@ -44,7 +59,7 @@ impl Config { | |
| Ok(default_config.merge_from(loaded_config)) | ||
| } | ||
|
|
||
| fn load_resolver_config(config_dir: impl AsRef<Path>) -> Result<resolver::Config> { | ||
| fn load_resolver_config(config_dir: impl AsRef<Path>) -> Result<resolver::Config, ConfigError> { | ||
| let resolver_path = config_dir.as_ref().join("resolver.yaml"); | ||
|
|
||
| //TODO: Fail with error if resolver config is missing | ||
|
|
@@ -56,7 +71,7 @@ impl Config { | |
| } | ||
| } | ||
|
|
||
| fn load_provider_configs(providers_dir: impl AsRef<Path>) -> Result<Vec<Provider>> { | ||
| fn load_provider_configs(providers_dir: impl AsRef<Path>) -> Result<Vec<Provider>, ConfigError> { | ||
| let providers_dir = providers_dir.as_ref(); | ||
| //TODO: Fail with error if providers config is missing | ||
| if !providers_dir.exists() { | ||
|
|
@@ -105,7 +120,7 @@ impl Config { | |
| debug!("Loaded Netcup provider config from {:?}", path); | ||
| } | ||
| _ => { | ||
| error!("Unknown provider config file: {}", path.display()); | ||
| return Err(ConfigError::UnknownProvider(path.display().to_string())); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -120,7 +135,7 @@ impl Config { | |
| Ok(configs) | ||
| } | ||
|
|
||
| fn load_dns_configs(dns_dir: impl AsRef<Path>) -> Result<Vec<dns::Type>> { | ||
| fn load_dns_configs(dns_dir: impl AsRef<Path>) -> Result<Vec<dns::Type>, ConfigError> { | ||
| let dns_dir = dns_dir.as_ref(); | ||
|
|
||
| //TODO: Fail with error if dns config is missing | ||
|
|
@@ -162,10 +177,7 @@ impl Config { | |
| configs.push(dns::Type::Netcup(config)); | ||
| debug!("Loaded Netcup DNS config from {:?}", path); | ||
| } else { | ||
| error!( | ||
| "Cannot determine DNS config type for file: {}", | ||
| path.display() | ||
| ); | ||
| return Err(ConfigError::UnknownDnsType(path.display().to_string())); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -174,7 +186,7 @@ impl Config { | |
| Ok(configs) | ||
| } | ||
|
|
||
| pub fn create_example_structure(config_dir: impl AsRef<Path>) -> Result<()> { | ||
| pub fn create_example_structure(config_dir: impl AsRef<Path>) -> Result<(), ConfigError> { | ||
| let config_dir = config_dir.as_ref(); | ||
|
|
||
| fs::create_dir_all(config_dir.join("providers"))?; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| use thiserror::Error; | ||
|
|
||
| use crate::provider::{hetzner, netcup, nitrado}; | ||
|
|
||
| /// Error type for all DNS provider operations. | ||
| /// | ||
| /// This enum wraps provider-specific errors and common failure cases, | ||
| /// providing a unified error type for the provider trait. | ||
| #[derive(Debug, Error)] | ||
| pub enum ProviderError { | ||
| /// Error from Hetzner DNS provider | ||
| #[error("Hetzner provider error: {0}")] | ||
| Hetzner(#[from] hetzner::Error), | ||
|
|
||
| /// Error from Nitrado DNS provider | ||
| #[error("Nitrado provider error: {0}")] | ||
| Nitrado(#[from] nitrado::Error), | ||
|
|
||
| /// Error from Netcup DNS provider | ||
| #[error("Netcup provider error: {0}")] | ||
| Netcup(#[from] netcup::Error), | ||
|
|
||
| /// HTTP request failed | ||
| #[error("HTTP request failed: {0}")] | ||
| Http(#[from] reqwest::Error), | ||
|
|
||
| /// JSON parsing error | ||
| #[error("JSON parsing error: {0}")] | ||
| Json(#[from] lum_libs::serde_json::Error), | ||
|
Kitt3120 marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// Invalid API key format for HTTP headers | ||
| #[error("Invalid API key: contains characters not allowed in HTTP headers")] | ||
| InvalidApiKey, | ||
| } | ||
|
Kitt3120 marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.