Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return full error response if error JSON does not contain "message" field #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
16 changes: 16 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}