Package list golang-github-go-kit-kit / f307e26
examples: stringsvc should properly return errors The error type doesn't JSON marshal. Fixes #121. Peter Bourgon 6 years ago
5 changed file(s) with 17 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
7474
7575 type uppercaseResponse struct {
7676 V string `json:"v"`
77 Err error `json:"err"`
77 Err string `json:"err"` // errors don't define JSON marshaling
7878 }
7979
8080 type countRequest struct {
108108 return func(ctx context.Context, request interface{}) (interface{}, error) {
109109 req := request.(uppercaseRequest)
110110 v, err := svc.Uppercase(req.S)
111 return uppercaseResponse{v, err}, nil
111 return uppercaseResponse{v, err.Error()}, nil
112112 }
113113 }
114114
482482 return "", err
483483 }
484484 resp := response.(uppercaseResponse)
485 return resp.V, resp.Err
485 if resp.Err != "" {
486 return resp.V, errors.New(resp.Err)
487 }
488 return resp.V, nil
486489 }
487490 ```
488491
5858 return func(ctx context.Context, request interface{}) (interface{}, error) {
5959 req := request.(uppercaseRequest)
6060 v, err := svc.Uppercase(req.S)
61 return uppercaseResponse{v, err}, nil
61 return uppercaseResponse{v, err.Error()}, nil
6262 }
6363 }
6464
9696
9797 type uppercaseResponse struct {
9898 V string `json:"v"`
99 Err error `json:"err"`
99 Err string `json:"err"` // errors don't define JSON marshaling
100100 }
101101
102102 type countRequest struct {
1212 return func(ctx context.Context, request interface{}) (interface{}, error) {
1313 req := request.(uppercaseRequest)
1414 v, err := svc.Uppercase(req.S)
15 return uppercaseResponse{v, err}, nil
15 return uppercaseResponse{v, err.Error()}, nil
1616 }
1717 }
1818
5050
5151 type uppercaseResponse struct {
5252 V string `json:"v"`
53 Err error `json:"err"`
53 Err string `json:"err"`
5454 }
5555
5656 type countRequest struct {
00 package main
11
22 import (
3 "errors"
34 "fmt"
45 "net/http"
56 "net/url"
5657 }
5758
5859 resp := response.(uppercaseResponse)
59 return resp.V, resp.Err
60 if resp.Err != "" {
61 return resp.V, errors.New(resp.Err)
62 }
63 return resp.V, nil
6064 }
6165
6266 func factory(ctx context.Context, qps int) loadbalancer.Factory {
1414 return func(ctx context.Context, request interface{}) (interface{}, error) {
1515 req := request.(uppercaseRequest)
1616 v, err := svc.Uppercase(req.S)
17 return uppercaseResponse{v, err}, nil
17 return uppercaseResponse{v, err.Error()}, nil
1818 }
1919 }
2020
6969
7070 type uppercaseResponse struct {
7171 V string `json:"v"`
72 Err error `json:"err"`
72 Err string `json:"err"`
7373 }
7474
7575 type countRequest struct {