SDK error revamp (#432)
SDK methods now return 1 error. To differentiate between Client errors and Server errors I've also implemented the AsServerError function which returns an *api.Error if it is an error originating from the remote server or nil otherwise.
This commit is contained in:
1 parent
94f9d952be
commit
fd0da998e8
53 files changed
+1512
-1635
No files matched your search
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/cmd/common"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/kr/pretty"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
@@ -248,25 +247,24 @@ func (c *Command) Run(args []string) int {
|
||||
existed := true
|
||||
var result api.GenericResult
|
||||
var listResult api.GenericListResult
|
||||
var apiErr *api.Error
|
||||
|
||||
switch c.Func {
|
||||
case "read":
|
||||
result, apiErr, err = hostsetClient.Read(c.Context, c.FlagId, opts...)
|
||||
result, err = hostsetClient.Read(c.Context, c.FlagId, opts...)
|
||||
case "delete":
|
||||
_, apiErr, err = hostsetClient.Delete(c.Context, c.FlagId, opts...)
|
||||
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
|
||||
_, err = hostsetClient.Delete(c.Context, c.FlagId, opts...)
|
||||
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
|
||||
existed = false
|
||||
apiErr = nil
|
||||
err = nil
|
||||
}
|
||||
case "list":
|
||||
listResult, apiErr, err = hostsetClient.List(c.Context, c.flagHostCatalogId, opts...)
|
||||
listResult, err = hostsetClient.List(c.Context, c.flagHostCatalogId, opts...)
|
||||
case "add-hosts":
|
||||
result, apiErr, err = hostsetClient.AddHosts(c.Context, c.FlagId, version, hosts, opts...)
|
||||
result, err = hostsetClient.AddHosts(c.Context, c.FlagId, version, hosts, opts...)
|
||||
case "remove-hosts":
|
||||
result, apiErr, err = hostsetClient.RemoveHosts(c.Context, c.FlagId, version, hosts, opts...)
|
||||
result, err = hostsetClient.RemoveHosts(c.Context, c.FlagId, version, hosts, opts...)
|
||||
case "set-hosts":
|
||||
result, apiErr, err = hostsetClient.SetHosts(c.Context, c.FlagId, version, hosts, opts...)
|
||||
result, err = hostsetClient.SetHosts(c.Context, c.FlagId, version, hosts, opts...)
|
||||
}
|
||||
|
||||
plural := "host set"
|
||||
@@ -274,13 +272,13 @@ func (c *Command) Run(args []string) int {
|
||||
plural = "host sets"
|
||||
}
|
||||
if err != nil {
|
||||
if api.AsServerError(err) != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
|
||||
return 1
|
||||
}
|
||||
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
|
||||
return 2
|
||||
}
|
||||
if apiErr != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
|
||||
return 1
|
||||
}
|
||||
|
||||
switch c.Func {
|
||||
case "delete":
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/common"
|
||||
"github.com/hashicorp/boundary/sdk/strutil"
|
||||
"github.com/kr/pretty"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
@@ -152,24 +151,23 @@ func (c *StaticCommand) Run(args []string) int {
|
||||
}
|
||||
|
||||
var result api.GenericResult
|
||||
var apiErr *api.Error
|
||||
|
||||
switch c.Func {
|
||||
case "create":
|
||||
result, apiErr, err = hostsetClient.Create(c.Context, c.flagHostCatalogId, opts...)
|
||||
result, err = hostsetClient.Create(c.Context, c.flagHostCatalogId, opts...)
|
||||
case "update":
|
||||
result, apiErr, err = hostsetClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
result, err = hostsetClient.Update(c.Context, c.FlagId, version, opts...)
|
||||
}
|
||||
|
||||
plural := "static-type host-set"
|
||||
if err != nil {
|
||||
if api.AsServerError(err) != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, err.Error()))
|
||||
return 1
|
||||
}
|
||||
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))
|
||||
return 2
|
||||
}
|
||||
if apiErr != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, pretty.Sprint(apiErr)))
|
||||
return 1
|
||||
}
|
||||
|
||||
set := result.GetItem().(*hostsets.HostSet)
|
||||
switch base.Format(c.UI) {
|
||||
|
||||
Reference in new issue
Block a user