github.com/cockroachdb/errors@v1.11.1/errutil/as.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Copyright 2019 The Cockroach 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 13 // implied. See the License for the specific language governing 14 // permissions and limitations under the License. 15 16 package errutil 17 18 import ( 19 "reflect" 20 21 "github.com/cockroachdb/errors/errbase" 22 ) 23 24 // As finds the first error in err's chain that matches the type to which target 25 // points, and if so, sets the target to its value and returns true. An error 26 // matches a type if it is assignable to the target type, or if it has a method 27 // As(interface{}) bool such that As(target) returns true. As will panic if target 28 // is not a non-nil pointer to a type which implements error or is of interface type. 29 // 30 // The As method should set the target to its value and return true if err 31 // matches the type to which target points. 32 // 33 // Note: this implementation differs from that of xerrors as follows: 34 // - it also supports recursing through causes with Cause(). 35 // - if it detects an API use error, its panic object is a valid error. 36 func As(err error, target interface{}) bool { 37 if target == nil { 38 panic(AssertionFailedf("errors.As: target cannot be nil")) 39 } 40 41 // We use introspection for now, of course when/if Go gets generics 42 // all this can go away. 43 val := reflect.ValueOf(target) 44 typ := val.Type() 45 if typ.Kind() != reflect.Ptr || val.IsNil() { 46 panic(AssertionFailedf("errors.As: target must be a non-nil pointer, found %T", target)) 47 } 48 if e := typ.Elem(); e.Kind() != reflect.Interface && !e.Implements(errorType) { 49 panic(AssertionFailedf("errors.As: *target must be interface or implement error, found %T", target)) 50 } 51 52 targetType := typ.Elem() 53 for c := err; c != nil; c = errbase.UnwrapOnce(c) { 54 if reflect.TypeOf(c).AssignableTo(targetType) { 55 val.Elem().Set(reflect.ValueOf(c)) 56 return true 57 } 58 if x, ok := c.(interface{ As(interface{}) bool }); ok && x.As(target) { 59 return true 60 } 61 62 // If at any point in the single cause chain including the top, 63 // we encounter a multi-cause chain, recursively explore it. 64 for _, cause := range errbase.UnwrapMulti(c) { 65 if As(cause, target) { 66 return true 67 } 68 } 69 } 70 71 return false 72 } 73 74 var errorType = reflect.TypeOf((*error)(nil)).Elem()