github.com/blend/go-sdk@v1.20240719.1/ex/is.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package ex
     9  
    10  import (
    11  	"errors"
    12  )
    13  
    14  // Is is a helper function that returns if an error is an ex.
    15  //
    16  // It will handle if the err is an exception, a multi-error or a regular error.
    17  // "Isness" is evaluated by if the class of the exception matches the class of the cause.
    18  //
    19  // Deprecated: Use [errors.Is]. Make sure `Is()` and `Unwrap()` are properly
    20  // implemented on your custom classes.
    21  func Is(err error, cause error) bool {
    22  	if err == nil || cause == nil {
    23  		return false
    24  	}
    25  	// If it's a ClassProvider, try comparing with the class first.
    26  	if typed, ok := err.(ClassProvider); ok && Is(typed.Class(), cause) {
    27  		return true
    28  	}
    29  	// Otherwise, use the native `errors.Is()`. Ex and Multi errors are handled in
    30  	// this case as they implement the `Is` method.
    31  	return errors.Is(err, cause)
    32  }