launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/watcher/helpers.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package watcher
     5  
     6  import (
     7  	"launchpad.net/errgo/errors"
     8  	"launchpad.net/tomb"
     9  )
    10  
    11  // Stopper is implemented by all watchers.
    12  type Stopper interface {
    13  	Stop() error
    14  }
    15  
    16  // Errer is implemented by all watchers.
    17  type Errer interface {
    18  	Err() error
    19  }
    20  
    21  // Stop stops the watcher. If an error is returned by the
    22  // watcher, t is killed with the error.
    23  func Stop(w Stopper, t *tomb.Tomb) {
    24  	if err := w.Stop(); err != nil {
    25  		t.Kill(err)
    26  	}
    27  }
    28  
    29  // MustErr returns the error with which w died.
    30  // Calling it will panic if w is still running or was stopped cleanly.
    31  func MustErr(w Errer) error {
    32  	err := w.Err()
    33  	if err == nil {
    34  		panic("watcher was stopped cleanly")
    35  	} else if errors.Cause(err) == tomb.ErrStillAlive {
    36  		panic("watcher is still running")
    37  	}
    38  	return err
    39  }