Codebase list golang-github-go-kit-kit / 2bccef9
transport/http: remove stuttering in Error Peter Bourgon 8 years ago
6 changed file(s) with 25 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
254254 case errAlreadyExists, errInconsistentIDs:
255255 return stdhttp.StatusBadRequest
256256 default:
257 if e, ok := err.(kithttp.TransportError); ok {
257 if e, ok := err.(kithttp.Error); ok {
258258 switch e.Domain {
259259 case kithttp.DomainDecode:
260260 return stdhttp.StatusBadRequest
7373
7474 req, err := http.NewRequest(c.method, c.tgt.String(), nil)
7575 if err != nil {
76 return nil, TransportError{Domain: DomainNewRequest, Err: err}
76 return nil, Error{Domain: DomainNewRequest, Err: err}
7777 }
7878
7979 if err = c.enc(ctx, req, request); err != nil {
80 return nil, TransportError{Domain: DomainEncode, Err: err}
80 return nil, Error{Domain: DomainEncode, Err: 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, TransportError{Domain: DomainDo, Err: err}
89 return nil, Error{Domain: DomainDo, Err: err}
9090 }
9191 if !c.bufferedStream {
9292 defer resp.Body.Close()
9494
9595 response, err := c.dec(ctx, resp)
9696 if err != nil {
97 return nil, TransportError{Domain: DomainDecode, Err: err}
97 return nil, Error{Domain: DomainDecode, Err: err}
9898 }
9999
100100 return response, nil
1717 DomainDecode = "Decode"
1818 )
1919
20 // TransportError is an error that occurred at some phase within the transport.
21 type TransportError struct {
20 // Error is an error that occurred at some phase within the transport.
21 type Error struct {
2222 // Domain is the phase in which the error was generated.
2323 Domain string
2424
2727 }
2828
2929 // Error implements the error interface.
30 func (e TransportError) Error() string {
30 func (e Error) Error() string {
3131 return fmt.Sprintf("%s: %s", e.Domain, e.Err)
3232 }
3636 t.Fatal("err == nil")
3737 }
3838
39 e, ok := err.(httptransport.TransportError)
39 e, ok := err.(httptransport.Error)
4040 if !ok {
41 t.Fatal("err is not of type github.com/go-kit/kit/transport/http.Err")
41 t.Fatal("err is not of type github.com/go-kit/kit/transport/http.Error")
4242 }
4343
4444 if want, have := sampleErr, e.Err; want != have {
4747 }
4848
4949 func ExampleErrOutput() {
50 sampleErr := errors.New("Oh no, an error")
51 err := httptransport.TransportError{Domain: httptransport.DomainDo, Err: sampleErr}
50 sampleErr := errors.New("oh no, an error")
51 err := httptransport.Error{Domain: httptransport.DomainDo, Err: sampleErr}
5252 fmt.Println(err)
5353 // Output:
54 // Do: Oh no, an error
54 // Do: oh no, an error
5555 }
8585 request, err := s.dec(ctx, r)
8686 if err != nil {
8787 s.logger.Log("err", err)
88 s.errorEncoder(ctx, TransportError{Domain: DomainDecode, Err: err}, w)
88 s.errorEncoder(ctx, Error{Domain: DomainDecode, Err: err}, w)
8989 return
9090 }
9191
9292 response, err := s.e(ctx, request)
9393 if err != nil {
9494 s.logger.Log("err", err)
95 s.errorEncoder(ctx, TransportError{Domain: DomainDo, Err: err}, w)
95 s.errorEncoder(ctx, Error{Domain: DomainDo, Err: err}, w)
9696 return
9797 }
9898
102102
103103 if err := s.enc(ctx, w, response); err != nil {
104104 s.logger.Log("err", err)
105 s.errorEncoder(ctx, TransportError{Domain: DomainEncode, Err: err}, w)
105 s.errorEncoder(ctx, Error{Domain: DomainEncode, Err: err}, w)
106106 return
107107 }
108108 }
109109
110 // ErrorEncoder is a function that's responsible for encoding an error to the ResponseWriter.
110 // ErrorEncoder is responsible for encoding an error to the ResponseWriter.
111 //
112 // In the server implementation, only kit/transport/http.Error values are ever
113 // passed to this function, so you might be tempted to have this function take
114 // one of those directly. But, users are encouraged to use custom ErrorEncoders
115 // to encode all HTTP errors to their clients, and so may want to pass and check
116 // for their own error types. See the example shipping/handling service.
111117 type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter)
112118
113119 func defaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter) {
114120 switch e := err.(type) {
115 case TransportError:
121 case Error:
116122 switch e.Domain {
117123 case DomainDecode:
118124 http.Error(w, err.Error(), http.StatusBadRequest)
5959 func TestServerErrorEncoder(t *testing.T) {
6060 errTeapot := errors.New("teapot")
6161 code := func(err error) int {
62 if e, ok := err.(httptransport.TransportError); ok && e.Err == errTeapot {
62 if e, ok := err.(httptransport.Error); ok && e.Err == errTeapot {
6363 return http.StatusTeapot
6464 }
6565 return http.StatusInternalServerError