Comments (5)
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.
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.
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.
@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.
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)
- go.mod: no matching versions for query "v1.2" HOT 3
- FindAll Regex HOT 1
- How to use selectors? HOT 1
- How can I find with class and get attribute? HOT 2
- Find Or HOT 2
- Find() ret Root field value Pointer maybe nil HOT 1
- Anything akin to BeautifulSoup's Comment? HOT 1
- Navigating to Parent HOT 2
- How to get http status code? HOT 1
- InnerHTML
- fatal error: concurrent map iteration and map write HOT 1
- findOnce break after the first child node.
- Is this possible to search by some attribute? HOT 1
- soup.HTMLParse() returning nil
- Should Text() return all sibling text?
- i found a bug in your soup
- Can anyone tell me how to send request through proxy? HOT 2
- how to set proxy with get request?
- invalid memory address or nil pointer dereference when chaining methods HOT 2
- incomplete docs (PostForm)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from soup.