Skip to content

Commit f6df77d

Browse files
committed
print non-nil json body on err
1 parent 1704e4b commit f6df77d

9 files changed

Lines changed: 17 additions & 8 deletions

File tree

internal/cli/announcements/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func deleteAnnouncement(id string, cfg *config.Config) {
3434
if body, err := client.SendRequestAndReadResponse(deleteUrl, true, http.MethodDelete, nil); err != nil {
3535
fmt.Fprintln(os.Stderr, "Error:", err)
3636
if body != nil {
37-
utils.PrettyPrintJSON(body)
37+
utils.PrettyPrintJSONErr(body)
3838
}
3939
} else {
4040
utils.PrettyPrintJSON(body)

internal/cli/announcements/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func getAnnouncement(uuid string, cfg *config.Config) {
3333
if body, err := client.SendRequestAndReadResponse(getUrl, false, http.MethodGet, nil); err != nil {
3434
fmt.Fprintln(os.Stderr, "Error:", err)
3535
if body != nil {
36-
utils.PrettyPrintJSON(body)
36+
utils.PrettyPrintJSONErr(body)
3737
}
3838
} else {
3939
utils.PrettyPrintJSON(body)

internal/cli/announcements/post.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ func postAnnouncement(cfg *config.Config) {
4444
bytes.NewBuffer(b)); err != nil {
4545
fmt.Fprintln(os.Stderr, "Error:", err)
4646
if body != nil {
47-
// NOTE: This isn't going to stderr. Should probably fix that at some point
48-
utils.PrettyPrintJSON(body)
47+
utils.PrettyPrintJSONErr(body)
4948
}
5049
} else {
5150
utils.PrettyPrintJSON(body)

internal/cli/announcements/put.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func putAnnouncements(id string, cfg *config.Config) {
6666
if body, err := client.SendRequestAndReadResponse(resourceUrl, true, http.MethodPut,
6767
bytes.NewBuffer(b)); err != nil {
6868
fmt.Fprintln(os.Stderr, "Error:", err)
69+
if body != nil {
70+
utils.PrettyPrintJSONErr(body)
71+
}
6972
} else {
7073
utils.PrettyPrintJSON(body)
7174
}

internal/cli/officers/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func deleteOfficer(id string, cfg *config.Config) {
3434
if body, err := client.SendRequestAndReadResponse(deleteUrl, true, http.MethodDelete, nil); err != nil {
3535
fmt.Fprintln(os.Stderr, "Error:", err)
3636
if body != nil {
37-
utils.PrettyPrintJSON(body)
37+
utils.PrettyPrintJSONErr(body)
3838
}
3939
} else {
4040
utils.PrettyPrintJSON(body)

internal/cli/officers/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func getOfficers(uuid string, cfg *config.Config) {
3131
if body, err := client.SendRequestAndReadResponse(getUrl, false, http.MethodGet, nil); err != nil {
3232
fmt.Fprintln(os.Stderr, "Error:", err)
3333
if body != nil {
34-
utils.PrettyPrintJSON(body)
34+
utils.PrettyPrintJSONErr(body)
3535
}
3636
} else {
3737
utils.PrettyPrintJSON(body)

internal/cli/officers/post.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func postOfficer(cfg *config.Config) {
4747
bytes.NewBuffer(b)); err != nil {
4848
fmt.Fprintln(os.Stderr, "Error:", err)
4949
if body != nil {
50-
utils.PrettyPrintJSON(body)
50+
utils.PrettyPrintJSONErr(body)
5151
}
5252
} else {
5353
utils.PrettyPrintJSON(body)

internal/cli/officers/put.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func putOfficer(id string, cfg *config.Config) {
3939
if body, err := client.SendRequestAndReadResponse(url, false, http.MethodGet, nil); err != nil {
4040
fmt.Fprintln(os.Stderr, "Error:", err)
4141
if body != nil {
42-
utils.PrettyPrintJSON(body)
42+
utils.PrettyPrintJSONErr(body)
4343
}
4444
return
4545
} else {

utils/output.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/tidwall/pretty"
78
)
@@ -11,3 +12,9 @@ func PrettyPrintJSON(json []byte) {
1112
colorfulJSON := pretty.Color(prettyJSON, nil)
1213
fmt.Print(string(colorfulJSON))
1314
}
15+
16+
func PrettyPrintJSONErr(json []byte) {
17+
prettyJSON := pretty.Pretty(json)
18+
colorfulJSON := pretty.Color(prettyJSON, nil)
19+
fmt.Fprint(os.Stderr, string(colorfulJSON))
20+
}

0 commit comments

Comments
 (0)