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 slightly improved main example #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

prasannavl
Copy link

A fairly common print_error implementation as a part of the main example.

A fairly common `print_error` implementation as a part of the main example.
eprintln!("caused by: {}", e);
cause = e.source();
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

This is a good start, but I'd rather avoid having people need to copy-paste this code in all of their projects. instead, see this comment where we discuss adding an iterator that does the core part of this function. Then, this would look like

fn main() {
    if let Err(e) = process_data() {
        eprintln!("error: {}", e);
        for cause in whatever_iterator_is_called(e) {
            eprintln!("caused by: {}", cause);
        }
    }
}

Copy link
Author

@prasannavl prasannavl Oct 9, 2019

Choose a reason for hiding this comment

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

If I remember correctly, there was a cargo package that kinda does this. Don't recall the specific name. It used the rust Termination trait and wrapped other Error types with an iterator around it. Basically, removed pretty much all the lines above into just a custom Result type. Anyway, that's too contextual for Snafu itself, as it only applies to main functions.

Of course, I like the idea of snafu providing an iterator directly somehow, but just thought it was a nice way to kick start it off for quick programs.. seen quite a few times, where people didn't use it correctly for simple programs, and made me do a face palm :)

Thought having this in the main README would least get people to copy paste it, and use it right, even though the iterator is a definitely a nicer way to do it.

Copy link
Owner

Choose a reason for hiding this comment

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

there was a cargo package

There's an unstable feature from the standard library (iter_chain / iter_sources) that basically I'd like to copy into SNAFU for now, add to the ErrorCompat trait, then replace with the standard library's version when it stabilizes.

Would you be interested in adding that support to improve this documentation?

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good. I'm not sure if I'll be able to make time this week though.. Just opening up an issue for that leaving this PR edited and currently blocked on it. Please feel free to close it if you feel it'd rather be tackled as one unit otherwise.. as it might also make sense to just close this without merging instead of holding on to this speculative small change for now.. :)

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

2 participants