github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/state/errors.go (about)

     1  // Copyright 2012-2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/charm.v6-unstable"
    12  	"gopkg.in/mgo.v2/txn"
    13  
    14  	"github.com/juju/juju/network"
    15  )
    16  
    17  // ErrCharmAlreadyUploaded is returned by UpdateUploadedCharm() when
    18  // the given charm is already uploaded and marked as not pending in
    19  // state.
    20  type ErrCharmAlreadyUploaded struct {
    21  	curl *charm.URL
    22  }
    23  
    24  func (e *ErrCharmAlreadyUploaded) Error() string {
    25  	return fmt.Sprintf("charm %q already uploaded", e.curl)
    26  }
    27  
    28  // IsCharmAlreadyUploadedError returns if the given error is
    29  // ErrCharmAlreadyUploaded.
    30  func IsCharmAlreadyUploadedError(err interface{}) bool {
    31  	if err == nil {
    32  		return false
    33  	}
    34  	// In case of a wrapped error, check the cause first.
    35  	value := err
    36  	cause := errors.Cause(err.(error))
    37  	if cause != nil {
    38  		value = cause
    39  	}
    40  	_, ok := value.(*ErrCharmAlreadyUploaded)
    41  	return ok
    42  }
    43  
    44  // ErrCharmRevisionAlreadyModified is returned when a pending or
    45  // placeholder charm is no longer pending or a placeholder, signaling
    46  // the charm is available in state with its full information.
    47  var ErrCharmRevisionAlreadyModified = fmt.Errorf("charm revision already modified")
    48  
    49  var ErrDead = fmt.Errorf("not found or dead")
    50  var errNotAlive = fmt.Errorf("not found or not alive")
    51  
    52  func onAbort(txnErr, err error) error {
    53  	if txnErr == txn.ErrAborted ||
    54  		errors.Cause(txnErr) == txn.ErrAborted {
    55  		return errors.Trace(err)
    56  	}
    57  	return errors.Trace(txnErr)
    58  }
    59  
    60  // ErrProviderIDNotUnique is a standard error to indicate the value specified
    61  // for a ProviderID field is not unique within the current model.
    62  type ErrProviderIDNotUnique struct {
    63  	duplicateIDs []string
    64  }
    65  
    66  func (e *ErrProviderIDNotUnique) Error() string {
    67  	idList := strings.Join(e.duplicateIDs, ", ")
    68  	return fmt.Sprintf("ProviderID(s) not unique: %s", idList)
    69  }
    70  
    71  // NewProviderIDNotUniqueError returns an instance of ErrProviderIDNotUnique
    72  // initialized with the given duplicate provider IDs.
    73  func NewProviderIDNotUniqueError(providerIDs ...network.Id) error {
    74  	stringIDs := make([]string, len(providerIDs))
    75  	for i, providerID := range providerIDs {
    76  		stringIDs[i] = string(providerID)
    77  	}
    78  	return newProviderIDNotUniqueErrorFromStrings(stringIDs)
    79  }
    80  
    81  func newProviderIDNotUniqueErrorFromStrings(providerIDs []string) error {
    82  	return &ErrProviderIDNotUnique{
    83  		duplicateIDs: providerIDs,
    84  	}
    85  }
    86  
    87  // IsProviderIDNotUniqueError returns if the given error or its cause is
    88  // ErrProviderIDNotUnique.
    89  func IsProviderIDNotUniqueError(err interface{}) bool {
    90  	if err == nil {
    91  		return false
    92  	}
    93  	// In case of a wrapped error, check the cause first.
    94  	value := err
    95  	cause := errors.Cause(err.(error))
    96  	if cause != nil {
    97  		value = cause
    98  	}
    99  	_, ok := value.(*ErrProviderIDNotUnique)
   100  	return ok
   101  }
   102  
   103  // ErrParentDeviceHasChildren is a standard error to indicate a network
   104  // link-layer device cannot be removed because other existing devices refer to
   105  // it as their parent.
   106  type ErrParentDeviceHasChildren struct {
   107  	parentName  string
   108  	numChildren int
   109  }
   110  
   111  func (e *ErrParentDeviceHasChildren) Error() string {
   112  	return fmt.Sprintf("parent device %q has %d children", e.parentName, e.numChildren)
   113  }
   114  
   115  func newParentDeviceHasChildrenError(parentName string, numChildren int) error {
   116  	return &ErrParentDeviceHasChildren{
   117  		parentName:  parentName,
   118  		numChildren: numChildren,
   119  	}
   120  }
   121  
   122  // IsParentDeviceHasChildrenError returns if the given error or its cause is
   123  // ErrParentDeviceHasChildren.
   124  func IsParentDeviceHasChildrenError(err interface{}) bool {
   125  	if err == nil {
   126  		return false
   127  	}
   128  	// In case of a wrapped error, check the cause first.
   129  	value := err
   130  	cause := errors.Cause(err.(error))
   131  	if cause != nil {
   132  		value = cause
   133  	}
   134  	_, ok := value.(*ErrParentDeviceHasChildren)
   135  	return ok
   136  }