1

Please note I'm using a nightly build of Rust 0.13.0

First off, I'm a beginner to Rust and am still in the process of consuming as much information as possible on the language. Throughout my consumption the one topic I've been able to find very little on is error handling. So, when I first attempted to use an external library in my code I became quickly stumped at how I should be using the material being returned to me.

To help explain my confusion, I will be referencing the rust-url library. Here is some sample code found in the documentation:

use url::{Url, InvalidIpv6Address};

assert!(Url::parse("http://[:::1]") == Err(InvalidIpv6Address))

This is pretty straight-forward to me. However, my next question was: what about the other errors? I looked further into the ParseError enum and found 15+ other types of errors that could be potentially produced by a malformed URL.

So, my question is, what is considered the proper way to handle all of these various conditions? Should I have a lengthy match that alerts out specialized messages for each one? Is there a way to consume them all at once?

I apologize if there is not a single answer to this question, but Google was not making it clear and I would prefer to have feedback on this before I proceed coding the rest of my project the wrong way.

2 Answers 2

3

The ParseError enum implements the Show trait, with a custom useful message for each variant, so when you get to the final step of actually handling the parse error (e.g. after manipulating the Result in whatever ways you see fit), you can just treat the error possibilities as a black box:

fn download(s: &str) {
    match Url::parse(s) {
        Ok(url) => { ... }
        Err(e) => {
            println!("Could not parse '{}'. {}.", s, e);
        }
    }
}

will print things like Could not parse 'http://[:::1]'. Invalid IPv6 address..

(I filed #43 about making the Show message lower-cased, so that it flows better in more complicated error messages.)

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. One more question: What would be the proper way to pass this error up the chain? I.E this particular call to Url::parse exists in the context of a library and I want to let the end-user know that URL parsing failed. Recommendation?
The recommended way to inferface errors between libraries etc. is still a work in progress; RFCs like #201 are proposals designed to improve things (that one is accepted but not implemented). For now, the two approaches are to just return url::ParseError (if that is the only error that can occur), or to return a wrapper type (e.g. a custom enum, or converting to a String) if there is more than one, or if there may be more than one in future.
That makes sense. Would you be able to show what the header of a function looks like that could return a ParseError?
@JoshuaGilman, the Url::parse function is one example, or e.g. fn foo() -> Result<uint, url::ParseError>.
Thanks, I saw that function header shortly after my comment and forgot to update it. Cheers.
2

Url::parse returns ParseResult<Url> which is defined as Result<Url, ParseError>, so you can make use of generic Result methods:

use url::{Url, InvalidIpv6Address};

assert!(Url::parse("http://[:::1]").is_err());

Result is Rust's preferred method for error handling, and has many convenience methods under the hood. For example, if you don't expect the failure you can use .unwrap() to make any failure fatal. When they don't suit your needs, you can also match against the Result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.