Skip to content

Commit

Permalink
fix: avoid all panics with proper error handling
Browse files Browse the repository at this point in the history
Make sure all the `unwrap()` is safe and has proper error handling
  • Loading branch information
azzamsa committed Apr 7, 2021
1 parent fb84831 commit 71a6e4b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dig many at once.
## Features

- Prevent invalid input before querying. Such invalid records type or configuration.
- No panics, good error handling.
- [more faster](docs/benchmark.md) compared to previous `digs.py`.
- Colourful output.
- Cross-platform.
Expand Down
4 changes: 2 additions & 2 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ fn get_address(nameserver: &str) -> Result<std::net::SocketAddr, DigsError> {

pub fn query(domain: &str, rtype: RTypes, nameserver: &str) -> Result<DnsResponse, DigsError> {
let address = get_address(nameserver)?;
let conn = UdpClientConnection::new(address).unwrap();
let conn = UdpClientConnection::new(address)?;
let client = SyncClient::new(conn);

let rtype = get_rtype(rtype);
let name = Name::from_str(&format!("{}.", domain)).unwrap();
let name = Name::from_str(&format!("{}.", domain))?;
let response = client.query(&name, DNSClass::IN, rtype);
match response {
Ok(resp) => Ok(resp),
Expand Down
5 changes: 5 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;

use thiserror::Error;
use trust_dns_client::error::ClientError;
use trust_dns_client::proto::error::ProtoError;

/// all possible errors returned by the app.
#[derive(Error, Debug)]
Expand All @@ -30,4 +31,8 @@ pub enum DigsError {
// All cases of `std::io::Error`.
#[error(transparent)]
IOError(#[from] std::io::Error),

// All cases of `trust_dns_proto::error::ProtoError`
#[error(transparent)]
Pro(#[from] ProtoError),
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn run() -> Result<()> {
let domain = utils::is_domain(matches.value_of("domain").unwrap())?;

// get rtype
// must be present. unwrap safe here
let rtype = matches.value_of_t("rtype").unwrap();

// get config file
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::path::{Path, PathBuf};

use crate::error::DigsError;

pub fn current_dir() -> PathBuf {
std::env::current_dir().unwrap()
pub fn current_dir() -> Result<PathBuf, DigsError> {
Ok(std::env::current_dir()?)
}

/// # Errors
Expand Down

0 comments on commit 71a6e4b

Please sign in to comment.