-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of errors, and some tests for errors
- Loading branch information
1 parent
eac256b
commit 303b9dd
Showing
4 changed files
with
58 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters