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"
|
||||
)
|
||||
@@ -157,19 +156,18 @@ 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 = authmethodClient.Read(c.Context, c.FlagId, opts...)
|
||||
result, err = authmethodClient.Read(c.Context, c.FlagId, opts...)
|
||||
case "delete":
|
||||
_, apiErr, err = authmethodClient.Delete(c.Context, c.FlagId, opts...)
|
||||
if apiErr != nil && apiErr.Status == int32(http.StatusNotFound) {
|
||||
_, err = authmethodClient.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 = authmethodClient.List(c.Context, c.FlagScopeId, opts...)
|
||||
listResult, err = authmethodClient.List(c.Context, c.FlagScopeId, opts...)
|
||||
}
|
||||
|
||||
plural := "auth method"
|
||||
@@ -177,13 +175,13 @@ func (c *Command) Run(args []string) int {
|
||||
plural = "auth methods"
|
||||
}
|
||||
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":
|
||||
|
||||
Reference in new issue
Block a user