forked from agtorre/go-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.go
40 lines (34 loc) · 835 Bytes
/
tasks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package waitgroup
import (
"fmt"
"log"
"net/http"
"strings"
"time"
)
// GetURL gets a url, and logs the time it took
func GetURL(url string) (*http.Response, error) {
start := time.Now()
log.Printf("getting %s", url)
resp, err := http.Get(url)
log.Printf("completed getting %s in %s", url, time.Since(start))
return resp, err
}
// CrawlError is our custom error type
// for aggregating errors
type CrawlError struct {
Errors []string
}
// Add adds another error
func (c *CrawlError) Add(err error) {
c.Errors = append(c.Errors, err.Error())
}
// Error implements the error interface
func (c *CrawlError) Error() string {
return fmt.Sprintf("All Errors: %s", strings.Join(c.Errors, ","))
}
// Valid can be used to determine if
// we should return this
func (c *CrawlError) Valid() bool {
return len(c.Errors) != 0
}