error.go raw

   1  package errors
   2  
   3  import "fmt"
   4  
   5  // Error is a base error that implement scw.SdkError
   6  type Error struct {
   7  	Str string
   8  	Err error
   9  }
  10  
  11  // Error implement standard xerror.Wrapper interface
  12  func (e *Error) Unwrap() error {
  13  	return e.Err
  14  }
  15  
  16  // Error implement standard error interface
  17  func (e *Error) Error() string {
  18  	str := "scaleway-sdk-go: " + e.Str
  19  	if e.Err != nil {
  20  		str += ": " + e.Err.Error()
  21  	}
  22  	return str
  23  }
  24  
  25  // IsScwSdkError implement SdkError interface
  26  func (e *Error) IsScwSdkError() {}
  27  
  28  // New creates a new error with that same interface as fmt.Errorf
  29  func New(format string, args ...any) *Error {
  30  	return &Error{
  31  		Str: fmt.Sprintf(format, args...),
  32  	}
  33  }
  34  
  35  // Wrap an error with additional information
  36  func Wrap(err error, format string, args ...any) *Error {
  37  	return &Error{
  38  		Err: err,
  39  		Str: fmt.Sprintf(format, args...),
  40  	}
  41  }
  42