Skip to content

Minor tweaks to networking code aligning more closely with best practices and the chapter text #123

@smasetty

Description

@smasetty

commit 849cd420a78bbb1a246a86097f108a67a4315abf (HEAD -> user/smasetty/codeTuning, 1st-edition)
Author: Sharat Masetty smasetty@microsoft.com
Date: Fri Jun 20 17:06:19 2025 +0530

refactor: use custom error types and simplify error handling

- DNS resolver now returns DnsError instead of Box<dyn Error>
- Leverage From trait for cleaner error propagation

diff --git a/ch8/ch8-mget/src/dns.rs b/ch8/ch8-mget/src/dns.rs
index 4d4fdf3..3d6395e 100644
--- a/ch8/ch8-mget/src/dns.rs
+++ b/ch8/ch8-mget/src/dns.rs
@@ -38,7 +38,7 @@ impl std::error::Error for DnsError {} // <1>
pub fn resolve(
dns_server_address: &str,
domain_name: &str,
-) -> Result<Optionstd::net::IpAddr, Box> {
+) -> Result<Optionstd::net::IpAddr, DnsError> {
let domain_name =
Name::from_ascii(domain_name)
.map_err(DnsError::ParseDomainName)?;
diff --git a/ch8/misc/wraperror2.rs b/ch8/misc/wraperror2.rs
index 645e5ca..0695ec4 100644
--- a/ch8/misc/wraperror2.rs
+++ b/ch8/misc/wraperror2.rs
@@ -29,8 +29,8 @@ impl Fromnet::AddrParseError for UpstreamError {
}

fn main() -> Result<(), UpstreamError> {

  • let _f = File::open("invisible.txt").map_err(UpstreamError::IO)?;
  • let _localhost = "::1".parse::().map_err(UpstreamError::Parsing)?;
  • let _f = File::open("invisible.txt")?;

  • let _localhost = "::1".parse::()?;

    Ok(())
    }
    \ No newline at end of file

Did not create a new PR as it was against the guidelines of this repository. Please consider pulling this change @timClicks . Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions