Git Product home page Git Product logo

Comments (2)

avitex avatar avitex commented on August 26, 2024

Heya @aWeinzierl,

I'm about to release a new version that improves error messages but the way to know what went wrong will be the same. Essentially a resolver/group of resolvers returns a resolution stream. When resolving a single address, any error a resolver returns is ignored and it attempts the next resolver. If you want to see what's going wrong then you'll need to pickup each error that happens from the stream. Enough text, now for an example:

use futures_util::{future, StreamExt, TryStreamExt};
use public_ip::{dns, http, Version};

#[tokio::main]
async fn main() {
    // List of resolvers to try and get an IP address from.
    let resolver = &[
        http::HTTP_WHATISMYIPADDRESS_COM_RESOLVER,
        dns::GOOGLE_DNS_TXT_RESOLVER,
    ];
    let addr = public_ip::resolve(resolver, Version::Any)
        // For each error in the stream we print it out to STDERR (console).
        .inspect_err(|err| eprintln!("resolver error: {}", err))
        // We filter out the errors and leave just the resolved addresses in the stream.
        .filter_map(|result| future::ready(result.ok()))
        // We get the first resolved address in the stream.
        .next()
        // Wait for the future to finish.
        .await
        // We remove the details of the resolution if we don't care about them.
        .map(|(addr, _details)| addr);

    dbg!(addr);
}

from rust-public-ip.

avitex avatar avitex commented on August 26, 2024

You can try this example now at https://github.com/avitex/rust-public-ip/blob/master/examples/errors.rs

from rust-public-ip.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.