github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli-plugins/manager/error.go (about)

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