Skip to content

Commit

Permalink
Merge pull request #113 from wallyworld/use-utils-passthrougherror
Browse files Browse the repository at this point in the history
#113

RcPassthroughError has been moved to utils repo. Delete the one from here and use the utils one instead.
  • Loading branch information
jujubot authored Feb 8, 2024
2 parents 7275770 + ffccfac commit 48cf985
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func IsErrSilent(err error) bool
IsErrSilent returns whether the error should be logged from cmd.Main.


## func IsRcPassthroughError
## func utils.IsRcPassthroughError
``` go
func IsRcPassthroughError(err error) bool
```
Expand All @@ -92,7 +92,7 @@ by the callers of a command. This way the logged output can also
be displayed otherwise, e.g. on the screen.


## func NewRcPassthroughError
## func utils.NewRcPassthroughError
``` go
func NewRcPassthroughError(code int) error
```
Expand Down Expand Up @@ -628,7 +628,7 @@ Write formats and outputs the value as directed by the --format and



## type RcPassthroughError
## type utils.RcPassthroughError
``` go
type RcPassthroughError struct {
Code int
Expand Down
30 changes: 3 additions & 27 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,6 @@ import (
"github.com/juju/utils/v4"
)

// RcPassthroughError indicates that a Juju plugin command exited with a
// non-zero exit code. This error is used to exit with the return code.
type RcPassthroughError struct {
Code int
}

// Error implements error.
func (e *RcPassthroughError) Error() string {
return fmt.Sprintf("subprocess encountered error code %v", e.Code)
}

// IsRcPassthroughError returns whether the error is an RcPassthroughError.
func IsRcPassthroughError(err error) bool {
_, ok := err.(*RcPassthroughError)
return ok
}

// NewRcPassthroughError creates an error that will have the code used at the
// return code from the cmd.Main function rather than the default of 1 if
// there is an error.
func NewRcPassthroughError(code int) error {
return &RcPassthroughError{code}
}

// ErrSilent can be returned from Run to signal that Main should exit with
// code 1 without producing error output.
var ErrSilent = errors.New("cmd: error out silently")
Expand All @@ -55,7 +31,7 @@ func IsErrSilent(err error) bool {
if err == ErrSilent {
return true
}
if _, ok := err.(*RcPassthroughError); ok {
if utils.IsRcPassthroughError(err) {
return true
}
return false
Expand Down Expand Up @@ -466,8 +442,8 @@ func Main(c Command, ctx *Context, args []string) int {
return rc
}
if err := c.Run(ctx); err != nil {
if IsRcPassthroughError(err) {
return err.(*RcPassthroughError).Code
if utils.IsRcPassthroughError(err) {
return err.(*utils.RcPassthroughError).Code
}
if err != ErrSilent {
WriteError(ctx.Stderr, err)
Expand Down
3 changes: 2 additions & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/juju/loggo/v2"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/v4"
gc "gopkg.in/check.v1"

"github.com/juju/gnuflag"
Expand Down Expand Up @@ -214,7 +215,7 @@ func (s *CmdSuite) TestZeroOrOneArgs(c *gc.C) {

func (s *CmdSuite) TestIsErrSilent(c *gc.C) {
c.Assert(cmd.IsErrSilent(cmd.ErrSilent), gc.Equals, true)
c.Assert(cmd.IsErrSilent(cmd.NewRcPassthroughError(99)), gc.Equals, true)
c.Assert(cmd.IsErrSilent(utils.NewRcPassthroughError(99)), gc.Equals, true)
c.Assert(cmd.IsErrSilent(fmt.Errorf("noisy")), gc.Equals, false)
}

Expand Down
3 changes: 2 additions & 1 deletion supercommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/loggo/v2"
"github.com/juju/utils/v4"
)

var logger = loggo.GetLogger("cmd")
Expand Down Expand Up @@ -549,7 +550,7 @@ func (c *SuperCommand) Run(ctx *Context) error {
logger.Debugf("error stack: \n%v", errors.ErrorStack(err))

// Err has been logged above, we can make the err silent so it does not log again in cmd/main
if !IsRcPassthroughError(err) {
if !utils.IsRcPassthroughError(err) {
err = ErrSilent
}
} else {
Expand Down

0 comments on commit 48cf985

Please sign in to comment.