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 exception on failed query for early validation and better debug #143

Open
vgavro opened this issue Jul 31, 2019 · 1 comment
Open

Add exception on failed query for early validation and better debug #143

vgavro opened this issue Jul 31, 2019 · 1 comment

Comments

@vgavro
Copy link

vgavro commented Jul 31, 2019

Consider this boilerplate code:

divs = selector.css('div.notexisted')
if not len(divs):
    raise ParseError(
        'Query %s failed on %s'
        % ('div.notexisted', shorten(selector.get(), 40))
    )

We may add following methods to raise ParseError (or FindError?) and do not break backward compatibility:

selector.xpath_or_error  # raises if query didn't match anything
selector.xpath_first  # returns first, raises if query didn't match anything
selector.xpath_one  # returns first, raises if query didn't match or return more than first element.
selector.css_*  # same

or add parameter to xpath and css methods:

match=False  # default, do not break backward compatibility
match=True  # raise ParseError if selector didn't return anything
match=int  # raise ParseError if returned elements length didn't matched

I prefer ParseError name (because of "Parsel") and second option as it's easier to implement.
For first option you may see as example https://github.com/vgavro/requests-client/blob/master/requests_client/lxml.py

Please give me feedback so I may work on pull request, or how do you solve problem with early validation? Thank you.

@Gallaecio
Copy link
Member

I agree with the problem statement, which can get really complicated as you chain multiple selectors, and selectors based on previous selectors.

I’m not sure about the proposed API, though, and at the moment I cannot think of a different API that I would like more either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants