modify enc and dec to be defined in a single line as per comment on
PR
add pre-existing Domain consts as per comment on
https://github.com/go-kit/kit/pull/232
rename transporthttp to httptrasnport
rename as per https://github.com/go-kit/kit/pull/232
rename Err to TransportErr
rename As per recommendation in https://github.com/go-kit/kit/pull/232
modify test case, to be more explicit
modify as per comment on https://github.com/go-kit/kit/pull/232
replace strings with their const equivilents
replace string with their const equivilents as defined in err.go
replace t.Log/t.Fail combo with t.Error
replace as per suggestion in https://github.com/go-kit/kit/pull/232
relocate testing to be with the other stdlib imports
relocate as per suggestion in https://github.com/go-kit/kit/pull/232
rename Domain consts to be shorter.
rename Domain consts as per suggestion in
https://github.com/go-kit/kit/pull/232
Ayiga
8 years ago
73 | 73 | |
74 | 74 | req, err := http.NewRequest(c.method, c.tgt.String(), nil) |
75 | 75 | if err != nil { |
76 | return nil, Err{"NewRequest", err} | |
76 | return nil, TransportError{DomainNewRequest, err} | |
77 | 77 | } |
78 | 78 | |
79 | 79 | if err = c.enc(req, request); err != nil { |
80 | return nil, Err{"Encode", err} | |
80 | return nil, TransportError{DomainEncode, err} | |
81 | 81 | } |
82 | 82 | |
83 | 83 | for _, f := range c.before { |
86 | 86 | |
87 | 87 | resp, err := ctxhttp.Do(ctx, c.client, req) |
88 | 88 | if err != nil { |
89 | return nil, Err{"Do", err} | |
89 | return nil, TransportError{DomainDo, err} | |
90 | 90 | } |
91 | 91 | if !c.bufferedStream { |
92 | 92 | defer resp.Body.Close() |
94 | 94 | |
95 | 95 | response, err := c.dec(resp) |
96 | 96 | if err != nil { |
97 | return nil, Err{"Decode", err} | |
97 | return nil, TransportError{DomainDecode, err} | |
98 | 98 | } |
99 | 99 | |
100 | 100 | return response, nil |
3 | 3 | "fmt" |
4 | 4 | ) |
5 | 5 | |
6 | // Err represents an Error occurred in the Client transport level. | |
7 | type Err struct { | |
6 | // These are some pre-generated constants that can be used to check against | |
7 | // for the DomainErrors. | |
8 | const ( | |
9 | // DomainNewRequest represents an error at the Request Generation | |
10 | // Scope. | |
11 | DomainNewRequest = "NewRequest" | |
12 | ||
13 | // DomainEncode represent an error that has occurred at the Encode | |
14 | // level of the request. | |
15 | DomainEncode = "Encode" | |
16 | ||
17 | // DomainDo represents an error that has occurred at the Do, or | |
18 | // execution phase of the request. | |
19 | DomainDo = "Do" | |
20 | ||
21 | // DomainDecode represents an error that has occured at the Decode | |
22 | // phase of the request. | |
23 | DomainDecode = "Decode" | |
24 | ) | |
25 | ||
26 | // TransportError represents an Error occurred in the Client transport level. | |
27 | type TransportError struct { | |
8 | 28 | // Domain represents the domain of the error encountered. |
9 | 29 | // Simply, this refers to the phase in which the error was |
10 | 30 | // generated |
4 | 4 | "fmt" |
5 | 5 | "net/http" |
6 | 6 | "net/url" |
7 | "testing" | |
7 | 8 | |
8 | 9 | "golang.org/x/net/context" |
9 | 10 | |
10 | transporthttp "github.com/go-kit/kit/transport/http" | |
11 | ||
12 | "testing" | |
11 | httptransport "github.com/go-kit/kit/transport/http" | |
13 | 12 | ) |
14 | 13 | |
15 | 14 | func TestClientEndpointEncodeError(t *testing.T) { |
16 | 15 | var ( |
17 | 16 | sampleErr = errors.New("Oh no, an error") |
18 | enc = func(r *http.Request, request interface{}) error { | |
19 | return sampleErr | |
20 | } | |
21 | dec = func(r *http.Response) (response interface{}, err error) { | |
22 | return nil, nil | |
23 | } | |
17 | enc = func(r *http.Request, request interface{}) error { return sampleErr } | |
18 | dec = func(r *http.Response) (response interface{}, err error) { return nil, nil } | |
24 | 19 | ) |
25 | 20 | |
26 | 21 | u := &url.URL{ |
29 | 24 | Path: "/does/not/matter", |
30 | 25 | } |
31 | 26 | |
32 | c := transporthttp.NewClient( | |
27 | c := httptransport.NewClient( | |
33 | 28 | "GET", |
34 | 29 | u, |
35 | 30 | enc, |
38 | 33 | |
39 | 34 | _, err := c.Endpoint()(context.Background(), nil) |
40 | 35 | if err == nil { |
41 | t.Log("err == nil") | |
42 | t.Fail() | |
36 | t.Error("err == nil") | |
43 | 37 | } |
44 | 38 | |
45 | e, ok := err.(transporthttp.Err) | |
39 | e, ok := err.(httptransport.Err) | |
46 | 40 | if !ok { |
47 | t.Log("err is not of type github.com/go-kit/kit/transport/http.Err") | |
48 | t.Fail() | |
41 | t.Error("err is not of type github.com/go-kit/kit/transport/http.Err") | |
49 | 42 | } |
50 | 43 | |
51 | if e.Err != sampleErr { | |
52 | t.Logf("e.Err != sampleErr, %s vs %s", e.Err, sampleErr) | |
53 | t.Fail() | |
44 | if want, have := sampleErr, e.Err; want != have { | |
45 | t.Error("want %v, have %v", want, have) | |
54 | 46 | } |
55 | 47 | } |
56 | 48 | |
57 | 49 | func ExampleErrOutput() { |
58 | 50 | sampleErr := errors.New("Oh no, an error") |
59 | err := transporthttp.Err{"Do", sampleErr} | |
51 | err := httptransport.Err{"Do", sampleErr} | |
60 | 52 | fmt.Println(err) |
61 | 53 | // Output: |
62 | 54 | // Do: Oh no, an error |