github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/jujud/agent/model/errors.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package model
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/worker/dependency"
    10  	"github.com/juju/juju/worker/lifeflag"
    11  )
    12  
    13  // ErrRemoved may be returned by some worker started from Manifolds to
    14  // indicate that the model under management no longer exists.
    15  var ErrRemoved = errors.New("model removed")
    16  
    17  // LifeFilter is used with the lifeflag manifolds -- which do not depend
    18  // on runFlag -- to return appropriate errors for consumption by the
    19  // enclosing dependency.Engine (and/or its IsFatal check).
    20  func LifeFilter(err error) error {
    21  	cause := errors.Cause(err)
    22  	switch cause {
    23  	case lifeflag.ErrNotFound:
    24  		return ErrRemoved
    25  	case lifeflag.ErrValueChanged:
    26  		return dependency.ErrBounce
    27  	}
    28  	return err
    29  }
    30  
    31  // IsFatal will probably be helpful when configuring a dependency.Engine
    32  // to run the result of Manifolds.
    33  func IsFatal(err error) bool {
    34  	return errors.Cause(err) == ErrRemoved
    35  }
    36  
    37  // WorstError will probably be helpful when configuring a dependency.Engine
    38  // to run the result of Manifolds.
    39  func WorstError(err, _ error) error {
    40  	// Doesn't matter if there's only one fatal error.
    41  	return err
    42  }
    43  
    44  // IgnoreErrRemoved returns nil if passed an error caused by ErrRemoved,
    45  // and otherwise returns the original error.
    46  func IgnoreErrRemoved(err error) error {
    47  	cause := errors.Cause(err)
    48  	if cause == ErrRemoved {
    49  		return nil
    50  	}
    51  	return err
    52  }