github.com/qiniu/x@v1.11.9/errors/go113.go (about) 1 //go:build go1.13 2 // +build go1.13 3 4 /* 5 Copyright 2022 Qiniu Limited (qiniu.com) 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package errors 21 22 import ( 23 "errors" 24 ) 25 26 // -------------------------------------------------------------------- 27 28 // Unwrap returns the result of calling the Unwrap method on err, if err's 29 // type contains an Unwrap method returning error. 30 // Otherwise, Unwrap returns nil. 31 func Unwrap(err error) error { 32 return errors.Unwrap(err) 33 } 34 35 // Is reports whether any error in err's chain matches target. 36 // 37 // The chain consists of err itself followed by the sequence of errors obtained by 38 // repeatedly calling Unwrap. 39 // 40 // An error is considered to match a target if it is equal to that target or if 41 // it implements a method Is(error) bool such that Is(target) returns true. 42 // 43 // An error type might provide an Is method so it can be treated as equivalent 44 // to an existing error. For example, if MyError defines 45 // 46 // func (m MyError) Is(target error) bool { return target == os.ErrExist } 47 // 48 // then Is(MyError{}, os.ErrExist) returns true. See syscall.Errno.Is for 49 // an example in the standard library. 50 func Is(err, target error) bool { 51 return errors.Is(err, target) 52 } 53 54 // As finds the first error in err's chain that matches target, and if so, sets 55 // target to that error value and returns true. Otherwise, it returns false. 56 // 57 // The chain consists of err itself followed by the sequence of errors obtained by 58 // repeatedly calling Unwrap. 59 // 60 // An error matches target if the error's concrete value is assignable to the value 61 // pointed to by target, or if the error has a method As(interface{}) bool such that 62 // As(target) returns true. In the latter case, the As method is responsible for 63 // setting target. 64 // 65 // An error type might provide an As method so it can be treated as if it were a 66 // a different error type. 67 // 68 // As panics if target is not a non-nil pointer to either a type that implements 69 // error, or to any interface type. 70 func As(err error, target interface{}) bool { 71 return errors.As(err, target) 72 } 73 74 // --------------------------------------------------------------------