Giter Club home page Giter Club logo

Comments (5)

anaskhan96 avatar anaskhan96 commented on August 12, 2024 2

Implementing a field on the Root object for handling errors sounds great. I had this thought yesterday though I didn't know it was in practical use.
I would be implementing this in the upcoming update.

from soup.

anaskhan96 avatar anaskhan96 commented on August 12, 2024

There is no way as of now, but a new function could be added, similar to Find(), just returning a boolean if found or not.

from soup.

lrstanley avatar lrstanley commented on August 12, 2024

The current method of how errors are handled seems to be very un-idiomatic. Catching panics should really not be something done inside of a package. I do believe that the approach @FM1337 mentioned should be how these are rewritten. Especially when a library uses logging without interfacing with the user, this is not a very good approach and is generally going to be frowned upon.

Implementing additional functions is one method of getting around this, however imho even if this is a breaking change, it should be rewritten to follow standardized go idioms.

from soup.

anaskhan96 avatar anaskhan96 commented on August 12, 2024

@lrstanley I plan on removing logging of the errors in an upcoming update. However, when it comes to error handling, returning a second argument as an error variable was the first thought that came to my mind, but it defeated the purpose of making the interface similar to BeautifulSoup (using Find().Find().Text(), conjunctively). So I'm still exploring ways on how to handle the error in a better manner.

from soup.

lrstanley avatar lrstanley commented on August 12, 2024

Although BeautifulSoup is a good tool and package for Python, mirroring it's methods and signatures doesn't quite make sense -- Python invokes quite a bit of magic behind the scenes, intercepting errors, routing them differently, etc. People expect that if there is going to be an error, they will be able to handle it accordingly. With the current available signatures that the package provides, it is not clear without directly looking over your code, what's going on behind the scenes. Even if the current behavior plans to stay, it should be clearly noted that these functions will use internal logging/not return errors, so the package documentation can be transparent to the user.

Shown in the following Go blog post, you see the following mentioned:

The convention in the Go libraries is that even when a package uses panic internally, its external API still presents explicit error return values.

I understand the want to be able to group conjunctive and successive calls between methods, so less boilerplate has to occur, which I think is still easily possible. I may suggest that of what some other libraries do. They implement a field on the object (in this case, Root), for example named Error, which is set on a copy of the Root struct. This way, they can do something like:

links := doc.Find("div", "id", "userIds").Find("something").FindAll("a")

if links.Error != nil {
    // Handle accordingly.
}

An example of a library doing this is gorm (although not the best library in itself, it is able to implement error handling while still having a pass-the-object style syntax.

I also see that soup.Get() has a signature to return an error, however it's not even used currently, and panics still are:
https://github.com/anaskhan96/soup/blob/master/soup.go#L34-L48

Thoughts/opinions?

from soup.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.