fix(cli) check error value from command func (#2286)

This commit is contained in:
Damian Debkowski authored and GitHub committed 2022-07-23 09:50:52 -07:00
1 parent 5117cf9548
commit 1e44ed3d92
38 files changed
+828 -372

No files matched your search

@@ -209,27 +209,25 @@ func (c *PasswordCommand) Run(args []string) int {
case "create":
createResult, err = authmethodsClient.Create(c.Context, "password", c.FlagScopeId, opts...)
if exitCode := c.checkFuncError(err); exitCode > 0 {
return exitCode
}
resp = createResult.GetResponse()
item = createResult.GetItem()
case "update":
updateResult, err = authmethodsClient.Update(c.Context, c.FlagId, version, opts...)
if exitCode := c.checkFuncError(err); exitCode > 0 {
return exitCode
}
resp = updateResult.GetResponse()
item = updateResult.GetItem()
}
resp, item, err = executeExtraPasswordActions(c, resp, item, err, authmethodsClient, version, opts)
if err != nil {
if apiErr := api.AsServerError(err); apiErr != nil {
var opts []base.Option
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural), opts...)
return base.CommandApiError
}
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return base.CommandCliError
if exitCode := c.checkFuncError(err); exitCode > 0 {
return exitCode
}
output, err := printCustomPasswordActionOutput(c)
@@ -258,6 +256,18 @@ func (c *PasswordCommand) Run(args []string) int {
return base.CommandSuccess
}
func (c *PasswordCommand) checkFuncError(err error) int {
if err == nil {
return 0
}
if apiErr := api.AsServerError(err); apiErr != nil {
c.PrintApiError(apiErr, fmt.Sprintf("Error from controller when performing %s on %s", c.Func, c.plural))
return base.CommandApiError
}
c.PrintCliError(fmt.Errorf("Error trying to %s %s: %s", c.Func, c.plural, err.Error()))
return base.CommandCliError
}
var (
extraPasswordActionsFlagsMapFunc = func() map[string][]string { return nil }
extraPasswordSynopsisFunc = func(*PasswordCommand) string { return "" }