Browse Source

feat: more meaningful error messages, close body on httpc requests (#2238)

* feat: more meaningful error messages, close body on httpc requests

* fix: test failure
Kevin Wan 2 years ago
parent
commit
a987512c7b
5 changed files with 26 additions and 5 deletions
  1. 2 0
      gateway/config.go
  2. 20 3
      gateway/server.go
  3. 2 0
      rest/httpc/responses.go
  4. 1 1
      zrpc/resolver/target.go
  5. 1 1
      zrpc/resolver/target_test.go

+ 2 - 0
gateway/config.go

@@ -27,6 +27,8 @@ type (
 
 	// Upstream is the configuration for an upstream.
 	Upstream struct {
+		// Name is the name of the upstream.
+		Name string `json:",optional"`
 		// Grpc is the target of the upstream.
 		Grpc zrpc.RpcClientConf
 		// ProtoSets is the file list of proto set, like [hello.pb].

+ 20 - 3
gateway/server.go

@@ -59,6 +59,10 @@ func (s *Server) Stop() {
 }
 
 func (s *Server) build() error {
+	if err := s.ensureUpstreamNames(); err != nil {
+		return err
+	}
+
 	return mr.MapReduceVoid(func(source chan<- interface{}) {
 		for _, up := range s.upstreams {
 			source <- up
@@ -68,13 +72,13 @@ func (s *Server) build() error {
 		cli := zrpc.MustNewClient(up.Grpc)
 		source, err := s.createDescriptorSource(cli, up)
 		if err != nil {
-			cancel(err)
+			cancel(fmt.Errorf("%s: %w", up.Name, err))
 			return
 		}
 
 		methods, err := internal.GetMethods(source)
 		if err != nil {
-			cancel(err)
+			cancel(fmt.Errorf("%s: %w", up.Name, err))
 			return
 		}
 
@@ -95,7 +99,7 @@ func (s *Server) build() error {
 		}
 		for _, m := range up.Mappings {
 			if _, ok := methodSet[m.RpcPath]; !ok {
-				cancel(fmt.Errorf("rpc method %s not found", m.RpcPath))
+				cancel(fmt.Errorf("%s: rpc method %s not found", up.Name, m.RpcPath))
 				return
 			}
 
@@ -162,6 +166,19 @@ func (s *Server) createDescriptorSource(cli zrpc.Client, up Upstream) (grpcurl.D
 	return source, nil
 }
 
+func (s *Server) ensureUpstreamNames() error {
+	for _, up := range s.upstreams {
+		target, err := up.Grpc.BuildTarget()
+		if err != nil {
+			return err
+		}
+
+		up.Name = target
+	}
+
+	return nil
+}
+
 func (s *Server) prepareMetadata(header http.Header) []string {
 	vals := internal.ProcessHeaders(header)
 	if s.processHeader != nil {

+ 2 - 0
rest/httpc/responses.go

@@ -25,6 +25,8 @@ func ParseHeaders(resp *http.Response, val interface{}) error {
 
 // ParseJsonBody parses the response body, which should be in json content type.
 func ParseJsonBody(resp *http.Response, val interface{}) error {
+	defer resp.Body.Close()
+
 	if withJsonBody(resp) {
 		return mapping.UnmarshalJsonReader(resp.Body, val)
 	}

+ 1 - 1
zrpc/resolver/target.go

@@ -15,6 +15,6 @@ func BuildDirectTarget(endpoints []string) string {
 
 // BuildDiscovTarget returns a string that represents the given endpoints with discov schema.
 func BuildDiscovTarget(endpoints []string, key string) string {
-	return fmt.Sprintf("%s://%s/%s", internal.DiscovScheme,
+	return fmt.Sprintf("%s://%s/%s", internal.EtcdScheme,
 		strings.Join(endpoints, internal.EndpointSep), key)
 }

+ 1 - 1
zrpc/resolver/target_test.go

@@ -13,5 +13,5 @@ func TestBuildDirectTarget(t *testing.T) {
 
 func TestBuildDiscovTarget(t *testing.T) {
 	target := BuildDiscovTarget([]string{"localhost:123", "localhost:456"}, "foo")
-	assert.Equal(t, "discov://localhost:123,localhost:456/foo", target)
+	assert.Equal(t, "etcd://localhost:123,localhost:456/foo", target)
 }