diff --git a/CHANGELOG.md b/CHANGELOG.md index 03bd3ff..e2d6928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## v1.2 +## v1.2.0 ### Added @@ -9,7 +9,7 @@ - Error will now be one of a standard set of errors defined by the package. Details about the error message have been moved to the ErrorDetails property of Root. -## v1.1 +## v1.1.0 ### Added diff --git a/examples/errors/errors.go b/examples/errors/errors.go new file mode 100644 index 0000000..c04f02e --- /dev/null +++ b/examples/errors/errors.go @@ -0,0 +1,33 @@ +// Errors happen. This example shows how to detect and handle some of them. + +package main + +import ( + "fmt" + "log" + + "github.com/anaskhan96/soup" +) + +func main() { + _, err := soup.Get("this url isn't real!") + if err != nil && err.(soup.Error).Type == soup.ErrInGetRequest { + // Handle as required! + } + + url := fmt.Sprintf("https://xkcd.com/50") + xkcd, err := soup.Get(url) + if err != nil { + // Handle it + } + xkcdSoup := soup.HTMLParse(xkcd) + links := xkcdSoup.Find("div", "id", "linkz") + if links.Error != nil && links.Error.(soup.Error).Type == soup.ErrElementNotFound { + log.Printf("Element not found: %v", links.Error) + } + // These error types were introduced in version 1.2.0, but just checking for err still works: + links = xkcdSoup.Find("div", "id", "links2") + if links.Error != nil { + log.Printf("Something happened: %s", links.Error) + } +} diff --git a/soup.go b/soup.go index 60fe860..7e5ae95 100644 --- a/soup.go +++ b/soup.go @@ -15,6 +15,7 @@ import ( "golang.org/x/net/html" ) +// ErrorType defines types of errors that are possible from soup type ErrorType int const ( diff --git a/soup_test.go b/soup_test.go index be511ca..62900a5 100644 --- a/soup_test.go +++ b/soup_test.go @@ -5,6 +5,8 @@ import ( "strconv" "strings" "testing" + + "github.com/stretchr/testify/assert" ) const testHTML = ` @@ -205,10 +207,27 @@ func TestFullText(t *testing.T) { } func TestFullTextEmpty(t *testing.T) { - //