~themue/juju-core/go-state-first-error-improvement

« back to all changes in this revision

Viewing changes to state/state.go

  • Committer: Frank Mueller
  • Date: 2012-06-11 13:58:48 UTC
  • Revision ID: themue@gmail.com-20120611135848-wt1cixweej7y13su
state: First proposal for an improvement of the error handling in state.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        key := machineKey(id)
47
47
        removeMachine := func(t *topology) error {
48
48
                if !t.HasMachine(key) {
49
 
                        return fmt.Errorf("machine not found")
 
49
                        return stateError(MachineError, nil, "machine not found")
50
50
                }
51
51
                hasUnits, err := t.MachineHasUnits(key)
52
52
                if err != nil {
53
53
                        return err
54
54
                }
55
55
                if hasUnits {
56
 
                        return fmt.Errorf("machine has units")
 
56
                        return stateError(MachineError, nil, "machine has units")
57
57
                }
58
58
                return t.RemoveMachine(key)
59
59
        }
60
60
        if err := retryTopologyChange(s.zk, removeMachine); err != nil {
61
 
                return fmt.Errorf("can't remove machine %d: %v", id, err)
 
61
                return stateError(MachineError, err, "can't remove machine %d", id)
62
62
        }
63
63
        return zkRemoveTree(s.zk, fmt.Sprintf("/machines/%s", key))
64
64
}
207
207
        // Remove the service from the topology.
208
208
        removeService := func(t *topology) error {
209
209
                if !t.HasService(svc.key) {
210
 
                        return stateChanged
 
210
                        return stateChangedError
211
211
                }
212
212
                t.RemoveService(svc.key)
213
213
                return nil
381
381
// RemoveRelation removes the relation.
382
382
func (s *State) RemoveRelation(relation *Relation) error {
383
383
        removeRelation := func(t *topology) error {
384
 
                _, err := t.Relation(relation.key)
385
 
                if err != nil {
386
 
                        return fmt.Errorf("can't remove relation: %v", err)
387
 
                }
388
384
                return t.RemoveRelation(relation.key)
389
385
        }
390
 
        // TODO: Improve high-level errors, no passing of low-level 
391
 
        // errors directly to the caller.
392
 
        return retryTopologyChange(s.zk, removeRelation)
 
386
        if err := retryTopologyChange(s.zk, removeRelation); err != nil {
 
387
                return stateError(RelationError, err, "can't remove relation")
 
388
        }
 
389
        return nil
393
390
}