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

Add new error handling section #674

Merged
merged 3 commits into from Dec 5, 2015

Conversation

mdinger
Copy link
Contributor

@mdinger mdinger commented Dec 1, 2015

I added a new error handling section which I based on the @BurntSushi error handling blog doc. In general I think his docs motivate error handling really well however at the end of the docs where they diverge from strings as errors, it wasn't quite as good. You're left wondering why you'd ever bother implementing the Error trait and From and Display when you only need Display. Or perhaps you need From also. Anyway, I tried a slightly different approach by trying to approach the error trait and others as basically optional depending on the purpose and needs of the library/binary. He probably said more in the case study but I never actually looked at that part (so it may be absolutely wonderful).

In some sense, this may be quite practical because the examples are entirely usable and runnable in comparison to the original. The original, while each is runnable, the playpen links are basically useless which wouldn't work in this case.

I originally didn't like that I had to write the result error handling sections by loading files but after the fact, it seems like a good result and I'm fairly happy with it.

@steveklabnik r?

@BurntSushi If you have any opinions/critiques, please add them.

I'm not sure who else I could cc. I could possibly rename some of the sections and perhaps add some more See alsos because I might not have added enough but I'm not sure.

Sorry it took so long to get this up. I had this mostly to this point for a while but was taking a break and doing other things for a couple weeks I guess.


This branch is currently hosted here. The error handling section is here.

@rust-highfive
Copy link

r? @steveklabnik

(rust_highfive has picked a reviewer for you, use r? to override)

fn setup() {
// Ignore the return value because we don't care about it.
let _ = File::create("a")
.and_then(|mut file| file.write_all(b"grape"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing important context, but I would be very careful about using let _ = .... Does using unwrap here work? If so, that would be much better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍, let _ is an antipattern imho

On Tue, Dec 1, 2015 at 7:05 PM, Andrew Gallant notifications@github.com
wrote:

In examples/error/option_with_result/combinator_combinations/result_try.rs
#674 (comment)
:

@@ -0,0 +1,49 @@
+use std::io::prelude::*;
+use std::fs::File;
+
+type Result = std::result::Result<T, String>;
+
+// Setup to make this work. Create two files with some info.
+fn setup() {

  • // Ignore the return value because we don't care about it.
  • let _ = File::create("a")
  •    .and_then(|mut file| file.write_all(b"grape"));
    

I might be missing important context, but I would be very careful about
using let _ = .... Does using unwrap here work? If so, that would be much
better.


Reply to this email directly or view it on GitHub
https://github.com/rust-lang/rust-by-example/pull/674/files#r46359722.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can fix that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@BurntSushi
Copy link
Member

@mdinger This looks pretty good! I will say though, that while I read through everything, I had a really hard time evaluating it because it's out of order in Github's diff view. It makes it difficult to see whether the flow of information works. Is there a simple way to view this in its published form?

@mdinger
Copy link
Contributor Author

mdinger commented Dec 2, 2015

@BurntSushi I just uploaded it here. It's just a copy so while most things seem correct, the editor syntax highlighting seems to be missing. Syntax highlighting still works properly normally so it's probably that I didn't copy something properly but I'm not sure what.

The new error handling section is here.

@mdinger
Copy link
Contributor Author

mdinger commented Dec 2, 2015

Thanks. If you see any pages that need a See also, let me know. Some probably do but I missed them.

DoubleError::Parse(ref e) => Some(e),
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my impl of error::Error. I think the above case should be None but wasn't sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

@mdinger
Copy link
Contributor Author

mdinger commented Dec 2, 2015

I updated the rendered site as well

@BurntSushi
Copy link
Member

@mdinger Thanks for uploading a rendered version of this! It was very helpful. I read through and things LGTM. Nice work. 👍

…ound. Recurse into fmt instead of calling to_string
@mdinger
Copy link
Contributor Author

mdinger commented Dec 4, 2015

Sweet! I committed some more fixes and updated the render. The commit message states what changed. I'm pretty happy with the result.

@BurntSushi Thanks. You've been tremendously helpful.

steveklabnik added a commit that referenced this pull request Dec 5, 2015
Add new error handling section
@steveklabnik steveklabnik merged commit 59ec3cf into rust-lang:master Dec 5, 2015
@steveklabnik
Copy link
Member

Thanks so much!

@mdinger mdinger deleted the error_handling branch December 5, 2015 16:48
@mdinger
Copy link
Contributor Author

mdinger commented Dec 5, 2015

Awesome! Fastest rbe review for a big PR ever!

@steveklabnik
Copy link
Member

As far as I'm concerned, that's due to @BurntSushi :)

On Dec 5, 2015, 12:09 -0500, mdingernotifications@github.com, wrote:

Awesome! Fastest rbe review for a big PR ever!


Reply to this email directly orview it on GitHub(#674 (comment)).

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

Successfully merging this pull request may close these issues.

None yet

4 participants