github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 // InvalidConfigValue indicates that one of the config values failed validation. 13 type InvalidConfigValue struct { 14 errors.Err 15 cause error 16 17 // Key is the OS env var corresponding to the field with the bad value. 18 Key string 19 20 // Value is the invalid value. 21 Value interface{} 22 23 // Reason is the underlying error. 24 Reason error 25 } 26 27 // IsInvalidConfigValue returns whether or not the provided error is 28 // an InvalidConfigValue (or caused by one). 29 func IsInvalidConfigValue(err error) bool { 30 if _, ok := err.(*InvalidConfigValue); ok { 31 return true 32 } 33 err = errors.Cause(err) 34 _, ok := err.(InvalidConfigValue) 35 return ok 36 } 37 38 // NewInvalidConfigValue returns a new InvalidConfigValue for the given 39 // info. If the provided reason is an error then Reason is set to that 40 // error. Otherwise a non-nil value is treated as a string and Reason is 41 // set to a non-nil value that wraps it. 42 func NewInvalidConfigValue(key string, value, reason interface{}) error { 43 var underlying error 44 switch reason := reason.(type) { 45 case error: 46 underlying = reason 47 default: 48 if reason != nil { 49 underlying = errors.Errorf("%v", reason) 50 } 51 } 52 err := &InvalidConfigValue{ 53 cause: errors.NewNotValid(underlying, "GCE config value"), 54 Key: key, 55 Value: value, 56 Reason: underlying, 57 } 58 err.Err = errors.NewErr("config value") 59 err.Err.SetLocation(1) 60 return err 61 } 62 63 // NewMissingConfigValue returns a new error for a missing config field. 64 func NewMissingConfigValue(key, field string) error { 65 return NewInvalidConfigValue(key, "", "missing "+field) 66 } 67 68 // Cause implements errors.causer. This is necessary so that 69 // errors.IsNotValid works. 70 func (err *InvalidConfigValue) Cause() error { 71 return err.cause 72 } 73 74 // Underlying implements errors.wrapper. 75 func (err InvalidConfigValue) Underlying() error { 76 return err.cause 77 } 78 79 // Error implements error. 80 func (err InvalidConfigValue) Error() string { 81 return fmt.Sprintf("invalid config value (%s) for %q: %v", err.Value, err.Key, err.Reason) 82 }