fix(cli): Status code on api error
When using the json output of the cli (`-format=json`), a successful call to the controller API would place the HTTP status code in the JSON as `status_code`. However, if there was an error from the API the status code would be placed in the JSON as `status`. In order to be consistent, this adds a `status_code` field to the error response case. For backwards compatibility it still populates `status` in the error case. This inconsistency was missed partially due to a bug in the CLI tests. The assertion on status code was not valid and would always be successful. Thus this also fixes the assertions and tests.
This commit is contained in:
17 files changed
+101
-62
No files matched your search
@@ -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"
|
||||
@@ -181,16 +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"`
|
||||
Status int `json:"status"`
|
||||
ApiError json.RawMessage `json:"api_error"`
|
||||
}{
|
||||
Context: contextStr,
|
||||
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:
|
||||
|
||||
Reference in new issue
Block a user