Skip to content

Commit

Permalink
Add example of errors, and some tests for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostlandr committed Nov 17, 2019
1 parent eac256b commit 303b9dd
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## v1.2
## v1.2.0

### Added

Expand All @@ -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

Expand Down
33 changes: 33 additions & 0 deletions examples/errors/errors.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions soup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"golang.org/x/net/html"
)

// ErrorType defines types of errors that are possible from soup
type ErrorType int

const (
Expand Down
25 changes: 22 additions & 3 deletions soup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

const testHTML = `
Expand Down Expand Up @@ -205,10 +207,27 @@ func TestFullText(t *testing.T) {
}

func TestFullTextEmpty(t *testing.T) {
// <div id="5"><h1><span></span></h1></div>
h1 := doc.Find("div", "id", "5").Find("h1")
// <div id="5"><h1><span></span></h1></div>
h1 := doc.Find("div", "id", "5").Find("h1")

if h1.FullText() != "" {
if h1.Error != nil {
assert.Equal(t, ErrElementNotFound, h1.Error.(Error).Type)
}
if h1.FullText() != "" {
t.Errorf("Wrong text: %s", h1.FullText())
}
}

func TestNewErrorReturnsInspectableError(t *testing.T) {
err := newError(ErrElementNotFound, "element not found")
assert.NotNil(t, err)
assert.Equal(t, ErrElementNotFound, err.Type)
assert.Equal(t, "element not found", err.Error())
}

func TestFindReturnsInspectableError(t *testing.T) {
r := doc.Find("bogus", "thing")
assert.IsType(t, Error{}, r.Error)
assert.Equal(t, "element `bogus` with attributes `thing` not found", r.Error.Error())
assert.Equal(t, ErrElementNotFound, r.Error.(Error).Type)
}

0 comments on commit 303b9dd

Please sign in to comment.