...

Source file src/errors/wrap.go

Documentation: errors

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package errors
     6  
     7  import (
     8  	"internal/reflectlite"
     9  )
    10  
    11  // Unwrap returns the result of calling the Unwrap method on err, if err's
    12  // type contains an Unwrap method returning error.
    13  // Otherwise, Unwrap returns nil.
    14  func Unwrap(err error) error {
    15  	u, ok := err.(interface {
    16  		Unwrap() error
    17  	})
    18  	if !ok {
    19  		return nil
    20  	}
    21  	return u.Unwrap()
    22  }
    23  
    24  // Is reports whether any error in err's chain matches target.
    25  //
    26  // The chain consists of err itself followed by the sequence of errors obtained by
    27  // repeatedly calling Unwrap.
    28  //
    29  // An error is considered to match a target if it is equal to that target or if
    30  // it implements a method Is(error) bool such that Is(target) returns true.
    31  //
    32  // An error type might provide an Is method so it can be treated as equivalent
    33  // to an existing error. For example, if MyError defines
    34  //
    35  //	func (m MyError) Is(target error) bool { return target == fs.ErrExist }
    36  //
    37  // then Is(MyError{}, fs.ErrExist) returns true. See syscall.Errno.Is for
    38  // an example in the standard library. An Is method should only shallowly
    39  // compare err and the target and not call Unwrap on either.
    40  func Is(err, target error) bool {
    41  	if target == nil {
    42  		return err == target
    43  	}
    44  
    45  	isComparable := reflectlite.TypeOf(target).Comparable()
    46  	for {
    47  		if isComparable && err == target {
    48  			return true
    49  		}
    50  		if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
    51  			return true
    52  		}
    53  		// TODO: consider supporting target.Is(err). This would allow
    54  		// user-definable predicates, but also may allow for coping with sloppy
    55  		// APIs, thereby making it easier to get away with them.
    56  		if err = Unwrap(err); err == nil {
    57  			return false
    58  		}
    59  	}
    60  }
    61  
    62  // As finds the first error in err's chain that matches target, and if one is found, sets
    63  // target to that error value and returns true. Otherwise, it returns false.
    64  //
    65  // The chain consists of err itself followed by the sequence of errors obtained by
    66  // repeatedly calling Unwrap.
    67  //
    68  // An error matches target if the error's concrete value is assignable to the value
    69  // pointed to by target, or if the error has a method As(interface{}) bool such that
    70  // As(target) returns true. In the latter case, the As method is responsible for
    71  // setting target.
    72  //
    73  // An error type might provide an As method so it can be treated as if it were a
    74  // different error type.
    75  //
    76  // As panics if target is not a non-nil pointer to either a type that implements
    77  // error, or to any interface type.
    78  func As(err error, target any) bool {
    79  	if target == nil {
    80  		panic("errors: target cannot be nil")
    81  	}
    82  	val := reflectlite.ValueOf(target)
    83  	typ := val.Type()
    84  	if typ.Kind() != reflectlite.Ptr || val.IsNil() {
    85  		panic("errors: target must be a non-nil pointer")
    86  	}
    87  	targetType := typ.Elem()
    88  	if targetType.Kind() != reflectlite.Interface && !targetType.Implements(errorType) {
    89  		panic("errors: *target must be interface or implement error")
    90  	}
    91  	for err != nil {
    92  		if reflectlite.TypeOf(err).AssignableTo(targetType) {
    93  			val.Elem().Set(reflectlite.ValueOf(err))
    94  			return true
    95  		}
    96  		if x, ok := err.(interface{ As(any) bool }); ok && x.As(target) {
    97  			return true
    98  		}
    99  		err = Unwrap(err)
   100  	}
   101  	return false
   102  }
   103  
   104  var errorType = reflectlite.TypeOf((*error)(nil)).Elem()
   105  

View as plain text