Skip to content

Commit

Permalink
fixup! fix(cli): Status code on api error
Browse files Browse the repository at this point in the history
  • Loading branch information
tmessi committed Feb 2, 2023
1 parent acd4db8 commit f95c0ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
37 changes: 26 additions & 11 deletions internal/cmd/base/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/plugins"
"github.com/hashicorp/boundary/api/scopes"
"github.com/hashicorp/boundary/version"
"github.com/mitchellh/cli"
"github.com/mitchellh/go-wordwrap"
"github.com/pkg/errors"
Expand Down Expand Up @@ -181,18 +182,32 @@ func (c *Command) PrintApiError(in *api.Error, contextStr string, opt ...Option)
opts := getOpts(opt...)
switch Format(c.UI) {
case "json":
output := struct {
Context string `json:"context,omitempty"`
StatusCode int `json:"status_code"`
Status int `json:"status"` // TODO: Remove this in 0.14.0
ApiError json.RawMessage `json:"api_error"`
}{
Context: contextStr,
StatusCode: in.Response().StatusCode(),
Status: in.Response().StatusCode(),
ApiError: in.Response().Body.Bytes(),
var b []byte
if version.SupportsFeature(version.Binary, version.IncludeStatusInCli) {
output := struct {
Context string `json:"context,omitempty"`
StatusCode int `json:"status_code"`
Status int `json:"status"`
ApiError json.RawMessage `json:"api_error"`
}{
Context: contextStr,
StatusCode: in.Response().StatusCode(),
Status: in.Response().StatusCode(),
ApiError: in.Response().Body.Bytes(),
}
b, _ = JsonFormatter{}.Format(output)
} else {
output := struct {
Context string `json:"context,omitempty"`
StatusCode int `json:"status_code"`
ApiError json.RawMessage `json:"api_error"`
}{
Context: contextStr,
StatusCode: in.Response().StatusCode(),
ApiError: in.Response().Body.Bytes(),
}
b, _ = JsonFormatter{}.Format(output)
}
b, _ := JsonFormatter{}.Format(output)
c.UI.Error(string(b))

default:
Expand Down
5 changes: 5 additions & 0 deletions version/feature_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Feature int
const (
UnknownFeature Feature = iota
MultiHopSessionFeature
IncludeStatusInCli
)

var featureMap map[Feature]MetadataConstraint
Expand Down Expand Up @@ -52,6 +53,10 @@ func init() {
Constraints: newConstraints(">= 0.1.0"), // This feature exists at 0.1.0 and above
}
*/
featureMap[IncludeStatusInCli] = MetadataConstraint{
MetaInfo: []Metadata{OSS, HCP},
Constraints: newConstraints("< 0.14.0"),
}
}

func metadataStringToMetadata(m string) Metadata {
Expand Down

0 comments on commit f95c0ef

Please sign in to comment.