Codebase list golang-github-go-kit-kit / 5e24cfe
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
3 changed file(s) with 37 addition(s) and 25 deletion(s). Raw diff Collapse all Expand all
7373
7474 req, err := http.NewRequest(c.method, c.tgt.String(), nil)
7575 if err != nil {
76 return nil, Err{"NewRequest", err}
76 return nil, TransportError{DomainNewRequest, err}
7777 }
7878
7979 if err = c.enc(req, request); err != nil {
80 return nil, Err{"Encode", err}
80 return nil, TransportError{DomainEncode, err}
8181 }
8282
8383 for _, f := range c.before {
8686
8787 resp, err := ctxhttp.Do(ctx, c.client, req)
8888 if err != nil {
89 return nil, Err{"Do", err}
89 return nil, TransportError{DomainDo, err}
9090 }
9191 if !c.bufferedStream {
9292 defer resp.Body.Close()
9494
9595 response, err := c.dec(resp)
9696 if err != nil {
97 return nil, Err{"Decode", err}
97 return nil, TransportError{DomainDecode, err}
9898 }
9999
100100 return response, nil
33 "fmt"
44 )
55
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 {
828 // Domain represents the domain of the error encountered.
929 // Simply, this refers to the phase in which the error was
1030 // generated
44 "fmt"
55 "net/http"
66 "net/url"
7 "testing"
78
89 "golang.org/x/net/context"
910
10 transporthttp "github.com/go-kit/kit/transport/http"
11
12 "testing"
11 httptransport "github.com/go-kit/kit/transport/http"
1312 )
1413
1514 func TestClientEndpointEncodeError(t *testing.T) {
1615 var (
1716 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 }
2419 )
2520
2621 u := &url.URL{
2924 Path: "/does/not/matter",
3025 }
3126
32 c := transporthttp.NewClient(
27 c := httptransport.NewClient(
3328 "GET",
3429 u,
3530 enc,
3833
3934 _, err := c.Endpoint()(context.Background(), nil)
4035 if err == nil {
41 t.Log("err == nil")
42 t.Fail()
36 t.Error("err == nil")
4337 }
4438
45 e, ok := err.(transporthttp.Err)
39 e, ok := err.(httptransport.Err)
4640 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")
4942 }
5043
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)
5446 }
5547 }
5648
5749 func ExampleErrOutput() {
5850 sampleErr := errors.New("Oh no, an error")
59 err := transporthttp.Err{"Do", sampleErr}
51 err := httptransport.Err{"Do", sampleErr}
6052 fmt.Println(err)
6153 // Output:
6254 // Do: Oh no, an error