diff --git a/api.go b/api.go index c5b8c2e..e0f46f9 100644 --- a/api.go +++ b/api.go @@ -151,6 +151,12 @@ func errMsg(b []byte) error { return err } + // if error response does not contain "message" field, + // return the full error JSON + if errResp.Message == "" { + return errors.New(string(b)) + } + return errors.New(errResp.Message) } diff --git a/api_test.go b/api_test.go index 9c86186..730c6a2 100644 --- a/api_test.go +++ b/api_test.go @@ -1864,3 +1864,19 @@ func TestCRUDSiteRequestRule(t *testing.T) { t.Fatal(err) } } + +func TestErrMsg(t *testing.T) { + cases := []struct { + in []byte + want string + }{ + {[]byte(`{"message": "an error message"}`), "an error message"}, + {[]byte(`{"detail": "an error message"}`), `{"detail": "an error message"}`}, + } + + for _, tt := range cases { + if err := errMsg(tt.in); err.Error() != tt.want { + t.Errorf("got error message = %q, want %q", err.Error(), tt.want) + } + } +}