vitess.io/vitess@v0.16.2/go/vt/topo/errors.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 topo 18 19 import ( 20 "errors" 21 "fmt" 22 ) 23 24 // ErrorCode is the error code for topo errors. 25 type ErrorCode int 26 27 // The following is the list of error codes. 28 const ( 29 NodeExists = ErrorCode(iota) 30 NoNode 31 NodeNotEmpty 32 Timeout 33 Interrupted 34 BadVersion 35 PartialResult 36 NoUpdateNeeded 37 NoImplementation 38 NoReadOnlyImplementation 39 ) 40 41 // Error represents a topo error. 42 type Error struct { 43 code ErrorCode 44 message string 45 } 46 47 // NewError creates a new topo error. 48 func NewError(code ErrorCode, node string) error { 49 var message string 50 switch code { 51 case NodeExists: 52 message = fmt.Sprintf("node already exists: %s", node) 53 case NoNode: 54 message = fmt.Sprintf("node doesn't exist: %s", node) 55 case NodeNotEmpty: 56 message = fmt.Sprintf("node not empty: %s", node) 57 case Timeout: 58 message = fmt.Sprintf("deadline exceeded: %s", node) 59 case Interrupted: 60 message = fmt.Sprintf("interrupted: %s", node) 61 case BadVersion: 62 message = fmt.Sprintf("bad node version: %s", node) 63 case PartialResult: 64 message = fmt.Sprintf("partial result: %s", node) 65 case NoUpdateNeeded: 66 message = fmt.Sprintf("no update needed: %s", node) 67 case NoImplementation: 68 message = fmt.Sprintf("no such topology implementation %s", node) 69 case NoReadOnlyImplementation: 70 message = fmt.Sprintf("no read-only topology implementation %s", node) 71 default: 72 message = fmt.Sprintf("unknown code: %s", node) 73 } 74 return Error{ 75 code: code, 76 message: message, 77 } 78 } 79 80 // Error satisfies error. 81 func (e Error) Error() string { 82 return e.message 83 } 84 85 // IsErrType returns true if the error has the specified ErrorCode. 86 func IsErrType(err error, code ErrorCode) bool { 87 var e Error 88 89 if errors.As(err, &e) { 90 return e.code == code 91 } 92 93 if e, ok := err.(Error); ok { 94 return e.code == code 95 } 96 97 return false 98 }