Skip to content

Commit

Permalink
feat: better error messsage
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Dec 21, 2022
1 parent bfd081a commit f069b3e
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 78 deletions.
202 changes: 195 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ lto = true
panic = 'abort'

[dependencies]
anyhow = "1.0"
colored = "2.0"
log = "0.4"
miette = { version = "5.5", features = ["fancy"] }
pretty_env_logger = "0.4"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"
Expand Down
6 changes: 4 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use clap::{Parser, ValueEnum};

#[derive(Parser)]
Expand All @@ -16,8 +18,8 @@ pub struct Opts {
pub rtype: RecordType,

/// Specify an alternate configuration file
#[arg(short = 'f', long = "file")]
pub config: Option<String>,
#[arg(short = 'f', long = "file", default_value_os_t = PathBuf::from("digs.toml"))]
pub config: PathBuf,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
Expand Down
11 changes: 9 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub struct Config {
}

pub fn read(path: &Path) -> Result<Config, Error> {
let file_content = fs::read_to_string(path)?;
let file_content = fs::read_to_string(path).map_err(|_| Error::ConfigNotFound {
path: path.to_path_buf(),
})?;
deserialize(&file_content)
}

Expand All @@ -29,5 +31,10 @@ pub fn read(path: &Path) -> Result<Config, Error> {
/// But this is not accurate. All the other apps that depend on toml.rs
/// share the same faith.
fn deserialize(content: &str) -> Result<Config, Error> {
toml::from_str(content).map_err(|e| Error::InvalidConfig { source: e })
match toml::from_str(content) {
Ok(config) => Ok(config),
Err(e) => Err(Error::InvalidConfig {
message: e.to_string(),
}),
}
}
11 changes: 5 additions & 6 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ pub fn query(domain: &str, rtype: RecordType, nameserver: &str) -> Result<DnsRes
let client = SyncClient::new(conn);

let name = Name::from_str(&format!("{}.", domain))?;
let response = client.query(&name, DNSClass::IN, rtype);
match response {
Ok(resp) => Ok(resp),
Err(err) => Err(Error::ForeignError(err)),
}
Ok(client.query(&name, DNSClass::IN, rtype)?)
}

/// Parse address string
fn get_address(nameserver: &str) -> Result<std::net::SocketAddr, Error> {
let address = format!("{}:53", nameserver).parse::<std::net::SocketAddr>();
match address {
Ok(addr) => Ok(addr),
Err(_) => Err(Error::InvalidIpAddress(nameserver.to_string())),
Err(_) => Err(Error::InvalidArgument(format!(
"Invalid IP Adrress `{}`",
&nameserver
))),
}
}
Loading

0 comments on commit f069b3e

Please sign in to comment.