Skip to content
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

How to get an error message ein case of failure? #9

Closed
aWeinzierl opened this issue Feb 13, 2021 · 2 comments
Closed

How to get an error message ein case of failure? #9

aWeinzierl opened this issue Feb 13, 2021 · 2 comments

Comments

@aWeinzierl
Copy link

I use the resolver the basic way.
The resolver works in principal.
However the resolver fails to return an IP-address (i.e. returns None) on a different environment.

Is there a way to view what went wrong?

Environment 1: Windows 10 Pro
Environment 2: WSL2 (Ubuntu 18.04

@avitex
Copy link
Owner

avitex commented Feb 24, 2021

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);
}

@avitex
Copy link
Owner

avitex commented Feb 24, 2021

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

@avitex avitex closed this as completed Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants