github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/provider/gce/google/errors.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package google
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  )
    11  
    12  // InvalidConfigValueError indicates that one of the config values failed validation.
    13  type InvalidConfigValueError struct {
    14  	errors.Err
    15  
    16  	// Key is the OS env var corresponding to the field with the bad value.
    17  	Key string
    18  
    19  	// Value is the invalid value.
    20  	Value interface{}
    21  }
    22  
    23  // IsInvalidConfigValueError returns whether or not the cause of
    24  // the provided error is a *InvalidConfigValueError.
    25  func IsInvalidConfigValueError(err error) bool {
    26  	_, ok := errors.Cause(err).(*InvalidConfigValueError)
    27  	return ok
    28  }
    29  
    30  // NewInvalidConfigValueError returns a new InvalidConfigValueError for the given
    31  // info. If the provided reason is an error then Reason is set to that
    32  // error. Otherwise a non-nil value is treated as a string and Reason is
    33  // set to a non-nil value that wraps it.
    34  func NewInvalidConfigValueError(key, value string, reason error) error {
    35  	err := &InvalidConfigValueError{
    36  		Err:   *errors.Mask(reason).(*errors.Err),
    37  		Key:   key,
    38  		Value: value,
    39  	}
    40  	err.Err.SetLocation(1)
    41  	return err
    42  }
    43  
    44  // Cause implements errors.Causer.Cause.
    45  func (err *InvalidConfigValueError) Cause() error {
    46  	return err
    47  }
    48  
    49  // NewMissingConfigValue returns a new error for a missing config field.
    50  func NewMissingConfigValue(key, field string) error {
    51  	return NewInvalidConfigValueError(key, "", errors.New("missing "+field))
    52  }
    53  
    54  // Error implements error.
    55  func (err InvalidConfigValueError) Error() string {
    56  	return fmt.Sprintf("invalid config value (%s) for %q: %v", err.Value, err.Key, &err.Err)
    57  }