github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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/tomb"
     8  )
     9  
    10  // Stopper is implemented by all watchers.
    11  type Stopper interface {
    12  	Stop() error
    13  }
    14  
    15  // Errer is implemented by all watchers.
    16  type Errer interface {
    17  	Err() error
    18  }
    19  
    20  // Stop stops the watcher. If an error is returned by the
    21  // watcher, t is killed with the error.
    22  func Stop(w Stopper, t *tomb.Tomb) {
    23  	if err := w.Stop(); err != nil {
    24  		t.Kill(err)
    25  	}
    26  }
    27  
    28  // MustErr returns the error with which w died.
    29  // Calling it will panic if w is still running or was stopped cleanly.
    30  func MustErr(w Errer) error {
    31  	err := w.Err()
    32  	if err == nil {
    33  		panic("watcher was stopped cleanly")
    34  	} else if err == tomb.ErrStillAlive {
    35  		panic("watcher is still running")
    36  	}
    37  	return err
    38  }