diff --git a/pkg/cmd/api/api.go b/pkg/cmd/api/api.go index 29b29532f..7c5eeca0e 100644 --- a/pkg/cmd/api/api.go +++ b/pkg/cmd/api/api.go @@ -366,13 +366,13 @@ func processResponse(resp *http.Response, opts *ApiOptions, headersOutputStream responseBody = io.TeeReader(responseBody, bodyCopy) } - if opts.FilterOutput != "" { + if opts.FilterOutput != "" && serverError == "" { // TODO: reuse parsed query across pagination invocations err = export.FilterJSON(opts.IO.Out, responseBody, opts.FilterOutput) if err != nil { return } - } else if opts.Template != "" { + } else if opts.Template != "" && serverError == "" { // TODO: reuse parsed template across pagination invocations err = template.Execute(responseBody) if err != nil { diff --git a/pkg/cmd/api/api_test.go b/pkg/cmd/api/api_test.go index 04da8cc29..25190ea5f 100644 --- a/pkg/cmd/api/api_test.go +++ b/pkg/cmd/api/api_test.go @@ -490,6 +490,20 @@ func Test_apiRun(t *testing.T) { stdout: "not a cat", stderr: ``, }, + { + name: "output template when REST error", + options: ApiOptions{ + Template: `{{.status}}`, + }, + httpResponse: &http.Response{ + StatusCode: 400, + Body: ioutil.NopCloser(bytes.NewBufferString(`{"message": "THIS IS FINE"}`)), + Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}}, + }, + err: cmdutil.SilentError, + stdout: `{"message": "THIS IS FINE"}`, + stderr: "gh: THIS IS FINE (HTTP 400)\n", + }, { name: "jq filter", options: ApiOptions{ @@ -504,6 +518,20 @@ func Test_apiRun(t *testing.T) { stdout: "Mona\nHubot\n", stderr: ``, }, + { + name: "jq filter when REST error", + options: ApiOptions{ + FilterOutput: `.[].name`, + }, + httpResponse: &http.Response{ + StatusCode: 400, + Body: ioutil.NopCloser(bytes.NewBufferString(`{"message": "THIS IS FINE"}`)), + Header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}}, + }, + err: cmdutil.SilentError, + stdout: `{"message": "THIS IS FINE"}`, + stderr: "gh: THIS IS FINE (HTTP 400)\n", + }, } for _, tt := range tests {