Skip to content

Commit

Permalink
fix: expose util for rest status return
Browse files Browse the repository at this point in the history
Closes: #3309
  • Loading branch information
rikatz committed Dec 11, 2023
1 parent 72fe969 commit 980d22d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions vapi/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ func (e *statusError) Error() string {
return fmt.Sprintf("%s %s: %s", e.res.Request.Method, e.res.Request.URL, e.res.Status)
}

func IsStatusError(err error, code int) bool {
statusErr, ok := err.(*statusError)
if !ok || statusErr == nil || statusErr.res == nil {
return false
}
return statusErr.res.StatusCode == code
}

// RawResponse may be used with the Do method as the resBody argument in order
// to capture the raw response data.
type RawResponse struct {
Expand Down
9 changes: 7 additions & 2 deletions vapi/rest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import (
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vapi/internal"
"github.com/vmware/govmomi/vapi/rest"
"github.com/vmware/govmomi/vim25"

_ "github.com/vmware/govmomi/vapi/simulator"
"github.com/vmware/govmomi/vim25"
)

func TestSession(t *testing.T) {
Expand Down Expand Up @@ -58,6 +57,12 @@ func TestSession(t *testing.T) {
if session == nil {
t.Fatal("expected non-nil session")
}

path := c.Resource("/xpto/bla")
err = c.Do(ctx, path.Request(http.MethodGet), nil)
if !rest.IsStatusError(err, http.StatusNotFound) {
t.Fatal("expecting error to be 'StatusNotFound'", err)
}
})
}

Expand Down

0 comments on commit 980d22d

Please sign in to comment.