-
Notifications
You must be signed in to change notification settings - Fork 11
/
example_http_test.go
70 lines (58 loc) · 1.6 KB
/
example_http_test.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package errtrace_test
import (
"fmt"
"io"
"net"
"net/http"
"strings"
"braces.dev/errtrace"
"braces.dev/errtrace/internal/tracetest"
)
func Example_http() {
tp := &http.Transport{Dial: rateLimitDialer}
client := &http.Client{Transport: tp}
ps := &PackageStore{
client: client,
}
_, err := ps.Get()
fmt.Printf("Error fetching packages: %+v\n", tracetest.MustClean(errtrace.FormatString(err)))
// Output:
// Error fetching packages: Get "http://example.com/packages.index": connect rate limited
//
// braces.dev/errtrace_test.rateLimitDialer
// /path/to/errtrace/example_http_test.go:3
// braces.dev/errtrace_test.(*PackageStore).updateIndex
// /path/to/errtrace/example_http_test.go:2
// braces.dev/errtrace_test.(*PackageStore).Get
// /path/to/errtrace/example_http_test.go:1
}
type PackageStore struct {
client *http.Client
packagesCached []string
}
func (ps *PackageStore) Get() ([]string, error) {
if ps.packagesCached != nil {
return ps.packagesCached, nil
}
packages, err := ps.updateIndex()
if err != nil {
return nil, errtrace.Wrap(err)
}
ps.packagesCached = packages
return packages, nil
}
func (ps *PackageStore) updateIndex() ([]string, error) {
resp, err := ps.client.Get("http://example.com/packages.index")
if err != nil {
return nil, errtrace.Wrap(err)
}
contents, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errtrace.Wrap(err)
}
return strings.Split(string(contents), ","), nil
}
func rateLimitDialer(network, addr string) (net.Conn, error) {
// for testing, always return an error.
return nil, errtrace.New("connect rate limited")
}