Skip to content

PasrseError and error Trait #247

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

Closed
darrell-roberts opened this issue May 22, 2019 · 9 comments
Closed

PasrseError and error Trait #247

darrell-roberts opened this issue May 22, 2019 · 9 comments

Comments

@darrell-roberts
Copy link

Hi,

I was wondering why the ParseError struct does not implement the std::error::Error trait? I was hoping to wrap it in a custom error enum type and use the "?" operator when calling parse functions. From what I've tried so far has not allowed me to do so.

@SimonSapin
Copy link
Member

I don’t think there’s a reason not to have that impl, it’s just that nobody bothered to add it so far. I personally find the Error trait not to be useful. Note that the ? operator does not involve the Error trait at al. It relies on the From trait to convert between error types. So the fix to your issue might be to add impl From<cssparser::ParseError> for YourErrorType {…}.

That said, I’d accept a PR to add impls of Error for ParseError and BasicParseError.

@darrell-roberts
Copy link
Author

Hi,

Your right that it does not need to implement the Error trait for the ? operator. What I did in my client code is create an generalized error enum with variants for all the different crate errors I would encounter. This enum then implements the error trait and delegates to the variants for display and error traits (description, cause). That way I have a common result type I create that would return my wrapper error enum for the Result Err variant.

I'll checkout the cssparser code and add the Error trait and see if I can get that working and then I'll send off the PR.

@Boscop
Copy link

Boscop commented Apr 10, 2020

Please add the impl of std::error::Error for ParseError :)
I'm using this crate with anyhow btw, and ? isn't working because this impl doesn't exist.

@jdm
Copy link
Member

jdm commented Apr 10, 2020

@Boscop #247 (comment) pointed out that Error is not needed for ? to work.

@Boscop
Copy link

Boscop commented Apr 10, 2020

@jdm Yes but it requires a custom error type and a From impl. I'd prefer if ParseError worked out of the box with anyhow::Result and ?.

@Boscop
Copy link

Boscop commented Apr 10, 2020

Even with a custom From impl, I still have to call .map_err(MyError::from)?, just ? isn't enough :(
And the ParseError only impls Debug, not Display, and doesn't seem to contain the selector that it errored on. So my From impl doesn't have that as context..

So it could be improved by implementing Display, std::error::Error and making its Display impl also print the selector that caused the error.
(Like reqwest includes the url that caused an error in the error's Display output, e.g. error sending request for url (https://...): connection closed before message completed)

@SimonSapin
Copy link
Member

print the selector that caused the error.

What do you mean by that? This library mostly only knows about tokens, not CSS Selectors.

@Boscop
Copy link

Boscop commented Apr 10, 2020

@SimonSapin Sorry, I'm using scraper: https://docs.rs/scraper/0.11.0/scraper/selector/struct.Selector.html#method.parse

So please ignore that point. The others are still valid though :)

@koutoftimer
Copy link

Why can't you just use thiserror which de facto is a standard? Issue originates in 2019 a lot have been changed since that time.

emilio pushed a commit that referenced this issue Apr 27, 2023
Previously the `ParseError` struct did not implement
`std::error::Error` or `std::fmt::Display`. This meant that `ParseError`
was not boxable into an "any error" box like `anyhow::Error`. This
commit adds Error and Display implementations for ParseError and
friends.

Fixes #247
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants