Format errors better for CLI (#552)

* Start making errors easier to read for Table based CLI UI.

* Add TODO about reporting "update_mask" related errors.
pull/560/head
Todd Knight 6 years ago committed by GitHub
parent d6c6aea179
commit 6c6fb7a4d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,9 +2,7 @@ package api
import (
"errors"
"fmt"
"net/http"
"strings"
"google.golang.org/grpc/codes"
)
@ -28,27 +26,7 @@ func AsServerError(in error) *Error {
// Error satisfies the error interface.
func (e *Error) Error() string {
res := fmt.Sprintf("Status: %d, Code: %q, Error: %q", e.Status, e.Code, e.Message)
var dets []string
if e.Details != nil {
if e.Details.ErrorId != "" {
dets = append(dets, fmt.Sprintf("error_id: %q", e.Details.ErrorId))
}
if e.Details.RequestId != "" {
dets = append(dets, fmt.Sprintf("request_id: %q", e.Details.RequestId))
}
if e.Details.TraceId != "" {
dets = append(dets, fmt.Sprintf("TraceId: %q", e.Details.TraceId))
}
for _, rf := range e.Details.RequestFields {
dets = append(dets, fmt.Sprintf("request_field: {name: %q, desc: %q}", rf.Name, rf.Description))
}
}
if len(dets) > 0 {
det := strings.Join(dets, ", ")
res = fmt.Sprintf("%s, Details: {%s}", res, det)
}
return res
return e.responseBody.String()
}
// Errors are considered the same iff they are both api.Errors and their statuses are the same.

@ -9,6 +9,7 @@ import (
"unicode"
"unicode/utf8"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/scopes"
"github.com/mitchellh/cli"
"github.com/mitchellh/go-wordwrap"
@ -133,6 +134,54 @@ func WrapMap(prefixSpaces, maxLengthOverride int, input map[string]interface{})
return strings.Join(ret, "\n")
}
func PrintApiError(in *api.Error) string {
nonAttributeMap := map[string]interface{}{
"Status": in.Status,
"Code": in.Code,
"Message": in.Message,
}
if in.Details != nil {
if in.Details.TraceId != "" {
nonAttributeMap["Trace ID"] = in.Details.TraceId
}
if in.Details.RequestId != "" {
nonAttributeMap["Request ID"] = in.Details.RequestId
}
if in.Details.ErrorId != "" {
nonAttributeMap["Error ID"] = in.Details.ErrorId
}
}
maxLength := MaxAttributesLength(nonAttributeMap, nil, nil)
ret := []string{
"",
"Error information:",
WrapMap(2, maxLength+2, nonAttributeMap),
}
if in.Details != nil {
if len(in.Details.RequestFields) > 0 {
ret = append(ret,
"",
" Field-specific Errors:",
)
for _, field := range in.Details.RequestFields {
if field.Name == "update_mask" {
// TODO: Report useful error messages related to "update_mask".
continue
}
ret = append(ret,
fmt.Sprintf(" Name: -%s", strings.ReplaceAll(field.Name, "_", "-")),
fmt.Sprintf(" Error: %s", field.Description),
)
}
}
}
return WrapForHelpText(ret)
}
// An output formatter for json output of an object
type JsonFormatter struct{}

@ -279,8 +279,8 @@ func (c *Command) Run(args []string) int {
plural = "accounts"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -204,8 +204,8 @@ func (c *PasswordCommand) Run(args []string) int {
plural := "password-type account"
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -127,8 +127,8 @@ func (c *PasswordCommand) Run(args []string) int {
"password": c.flagPassword,
})
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authentication: %s", err.Error()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authentication: %s", base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to perform authentication: %s", err.Error()))

@ -175,8 +175,8 @@ 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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -194,8 +194,8 @@ func (c *PasswordCommand) Run(args []string) int {
plural := "password-type auth-method"
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -112,8 +112,8 @@ func (c *Command) Run(args []string) int {
plural = "auth tokens"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -193,8 +193,8 @@ func (c *Command) Run(args []string) int {
plural = "groups"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -175,8 +175,8 @@ func (c *Command) Run(args []string) int {
plural = "host catalogs"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -150,8 +150,8 @@ func (c *StaticCommand) Run(args []string) int {
plural := "static-type host-catalog"
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -173,8 +173,8 @@ func (c *Command) Run(args []string) int {
plural = "hosts"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -178,8 +178,8 @@ func (c *StaticCommand) Run(args []string) int {
plural := "static-type host"
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -265,8 +265,8 @@ 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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -148,8 +148,8 @@ func (c *StaticCommand) Run(args []string) int {
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -456,8 +456,8 @@ func (c *Command) Run(args []string) (retCode int) {
sar, err := targetClient.AuthorizeSession(c.Context, c.flagTargetId, opts...)
if err != nil {
if api.AsServerError(err) != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authorize-session against target: %s", err.Error()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing authorize-session against target: %s", base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to authorize a session against target: %s", err.Error()))

@ -244,8 +244,8 @@ func (c *Command) Run(args []string) int {
plural = "roles"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -171,8 +171,8 @@ func (c *Command) Run(args []string) int {
plural = "scopes"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -130,8 +130,8 @@ func (c *Command) Run(args []string) int {
plural = "sessions"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -294,8 +294,8 @@ func (c *Command) Run(args []string) int {
plural = "a session against target"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -222,8 +222,8 @@ func (c *TcpCommand) Run(args []string) int {
plural := "tcp-type target"
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

@ -193,8 +193,8 @@ func (c *Command) Run(args []string) int {
plural = "users"
}
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()))
if apiErr := api.AsServerError(err); apiErr != nil {
c.UI.Error(fmt.Sprintf("Error from controller when performing %s on %s: %s", c.Func, plural, base.PrintApiError(apiErr)))
return 1
}
c.UI.Error(fmt.Sprintf("Error trying to %s %s: %s", c.Func, plural, err.Error()))

Loading…
Cancel
Save