vitess.io/vitess@v0.16.2/go/vt/topo/etcd2topo/error.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package etcd2topo 18 19 import ( 20 "errors" 21 22 "context" 23 24 "go.etcd.io/etcd/api/v3/v3rpc/rpctypes" 25 "google.golang.org/grpc/codes" 26 "google.golang.org/grpc/status" 27 28 "vitess.io/vitess/go/vt/topo" 29 ) 30 31 // Errors specific to this package. 32 var ( 33 // ErrBadResponse is returned from this package if the response from the etcd 34 // server does not contain the data that the API promises. The etcd client 35 // unmarshals JSON from the server into a Response struct that uses pointers, 36 // so we need to check for nil pointers, or else a misbehaving etcd could 37 // cause us to panic. 38 ErrBadResponse = errors.New("etcd request returned success, but response is missing required data") 39 ) 40 41 // convertError converts an etcd error into a topo error. All errors 42 // are either application-level errors, or context errors. 43 func convertError(err error, nodePath string) error { 44 if err == nil { 45 return nil 46 } 47 48 if typeErr, ok := err.(rpctypes.EtcdError); ok { 49 switch typeErr.Code() { 50 case codes.NotFound: 51 return topo.NewError(topo.NoNode, nodePath) 52 case codes.Unavailable, codes.DeadlineExceeded: 53 // The etcd2 client library may return this error: 54 // grpc.Errorf(codes.Unavailable, 55 // "etcdserver: request timed out") which seems to be 56 // misclassified, it should be using 57 // codes.DeadlineExceeded. All timeouts errors 58 // seem to be using the codes.Unavailable 59 // category. So changing all of them to ErrTimeout. 60 // The other reasons for codes.Unavailable are when 61 // etcd primary election is failing, so timeout 62 // also sounds reasonable there. 63 return topo.NewError(topo.Timeout, nodePath) 64 } 65 return err 66 } 67 68 if s, ok := status.FromError(err); ok { 69 // This is a gRPC error. 70 switch s.Code() { 71 case codes.NotFound: 72 return topo.NewError(topo.NoNode, nodePath) 73 case codes.Canceled: 74 return topo.NewError(topo.Interrupted, nodePath) 75 case codes.DeadlineExceeded: 76 return topo.NewError(topo.Timeout, nodePath) 77 default: 78 return err 79 } 80 } 81 82 switch err { 83 case context.Canceled: 84 return topo.NewError(topo.Interrupted, nodePath) 85 case context.DeadlineExceeded: 86 return topo.NewError(topo.Timeout, nodePath) 87 default: 88 return err 89 } 90 }