-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ForcedOperation.FatalError() and use to replace oft replicated
error checking sequence where possible.
- Loading branch information
Showing
7 changed files
with
115 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2019 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package state | ||
|
||
import "time" | ||
|
||
// ForcedOperation that allows accumulation of operational errors and | ||
// can be forced. | ||
type ForcedOperation struct { | ||
// Force controls whether or not the removal of a unit | ||
// will be forced, i.e. ignore operational errors. | ||
Force bool | ||
|
||
// Errors contains errors encountered while applying this operation. | ||
// Generally, these are non-fatal errors that have been encountered | ||
// during, say, force. They may not have prevented the operation from being | ||
// aborted but the user might still want to know about them. | ||
Errors []error | ||
|
||
// MaxWait specifies the amount of time that each step in relation destroy process | ||
// will wait before forcing the next step to kick-off. This parameter | ||
// only makes sense in combination with 'force' set to 'true'. | ||
MaxWait time.Duration | ||
} | ||
|
||
// AddError adds an error to the collection of errors for this operation. | ||
func (op *ForcedOperation) AddError(one ...error) { | ||
op.Errors = append(op.Errors, one...) | ||
} | ||
|
||
// FatalError returns true if the err is not nil and Force is false. | ||
// If the error is not nil, it's added to the slice of errors for the | ||
// operation. | ||
func (op *ForcedOperation) FatalError(err error) bool { | ||
if err != nil { | ||
if !op.Force { | ||
return true | ||
} | ||
op.Errors = append(op.Errors, err) | ||
} | ||
return false | ||
} | ||
|
||
// LastError returns last added error for this operation. | ||
func (op *ForcedOperation) LastError() error { | ||
if len(op.Errors) == 0 { | ||
return nil | ||
} | ||
return op.Errors[len(op.Errors)-1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.