Skip to content

Commit

Permalink
add description about error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotwutingfeng committed Jul 17, 2022
1 parent ebf7309 commit e76ac5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ res, _ := extractor.Extract(fasttld.URLParams{URL: url, ConvertURLToPunyCode: tr
|----------|----------|-----------|-------------|--------|------------------|------|------|
| https:// | | hello | xn--rhqv96g | com | xn--rhqv96g.com | | |

### Parsing errors

`Extract()` returns an error if the parser detects the URL as invalid.

```go
extractor, _ := fasttld.New(fasttld.SuffixListParams{})
url := "https://example!.com" // invalid characters in hostname
color.New().Println("The following line should be an error message")
if _, err := extractor.Extract(fasttld.URLParams{URL: url}); err != nil {
color.New(color.FgHiRed, color.Bold).Print("Error: ")
color.New(color.FgHiWhite).Println(err)
}
```

## Testing

```sh
Expand Down Expand Up @@ -282,7 +296,7 @@ Benchmarks performed on AMD Ryzen 7 5800X, Manjaro Linux.

### Why not split on "." and take the last element instead?

Splitting on "." and taking the last element only works for simple TLDs like `.com`, but not more complex ones like `oseto.nagasaki.jp`.
Splitting on "." and taking the last element only works for simple TLDs like `com`, but not more complex ones like `oseto.nagasaki.jp`.

### TLD tries

Expand Down
10 changes: 10 additions & 0 deletions examples/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ func main() {
res, _ = extractor.Extract(fasttld.URLParams{URL: url, ConvertURLToPunyCode: false})
color.New(fontStyle...).Println("No Punycode")
fasttld.PrintRes(url, res)

// Parsing errors
url = "https://example!.com" // No spaces allowed in hostname

color.New(fontStyle...).Println("Parsing errors")
color.New().Println("The following line should be an error message")
if _, err := extractor.Extract(fasttld.URLParams{URL: url}); err != nil {
color.New(color.FgHiRed, color.Bold).Print("Error: ")
color.New(color.FgHiWhite).Println(err)
}
}

0 comments on commit e76ac5a

Please sign in to comment.