github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli-plugins/manager/error.go (about)

     1  // FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
     2  //go:build go1.19
     3  
     4  package manager
     5  
     6  import (
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // pluginError is set as Plugin.Err by NewPlugin if the plugin
    11  // candidate fails one of the candidate tests. This exists primarily
    12  // to implement encoding.TextMarshaller such that rendering a plugin as JSON
    13  // (e.g. for `docker info -f '{{json .CLIPlugins}}'`) renders the Err
    14  // field as a useful string and not just `{}`. See
    15  // https://github.com/golang/go/issues/10748 for some discussion
    16  // around why the builtin error type doesn't implement this.
    17  type pluginError struct {
    18  	cause error
    19  }
    20  
    21  // Error satisfies the core error interface for pluginError.
    22  func (e *pluginError) Error() string {
    23  	return e.cause.Error()
    24  }
    25  
    26  // Cause satisfies the errors.causer interface for pluginError.
    27  func (e *pluginError) Cause() error {
    28  	return e.cause
    29  }
    30  
    31  // Unwrap provides compatibility for Go 1.13 error chains.
    32  func (e *pluginError) Unwrap() error {
    33  	return e.cause
    34  }
    35  
    36  // MarshalText marshalls the pluginError into a textual form.
    37  func (e *pluginError) MarshalText() (text []byte, err error) {
    38  	return []byte(e.cause.Error()), nil
    39  }
    40  
    41  // wrapAsPluginError wraps an error in a pluginError with an
    42  // additional message, analogous to errors.Wrapf.
    43  func wrapAsPluginError(err error, msg string) error {
    44  	if err == nil {
    45  		return nil
    46  	}
    47  	return &pluginError{cause: errors.Wrap(err, msg)}
    48  }
    49  
    50  // NewPluginError creates a new pluginError, analogous to
    51  // errors.Errorf.
    52  func NewPluginError(msg string, args ...any) error {
    53  	return &pluginError{cause: errors.Errorf(msg, args...)}
    54  }