Skip to content

Commit

Permalink
client-go: add an Error() function on Request
Browse files Browse the repository at this point in the history
Requests can accumulate errors with no obvious indication, e.g. if
their primary purpose is to construct a URL: URL() itself doesn't
return an error if r.err is non-nil.

Instead of changing URL() to return an error, which has quite a large
impact, add an Error() function and indicate on URL() that it should
be checked.

Signed-off-by: Stephen Kitt <skitt@redhat.com>

Kubernetes-commit: f69c1c47463ff70ad61adf6f38c4d5b7373e9d0a
  • Loading branch information
skitt authored and k8s-publishing-bot committed Feb 10, 2023
1 parent d462933 commit 22e2a9b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,13 @@ func (r *Request) Body(obj interface{}) *Request {
return r
}

// URL returns the current working URL.
// Error returns any error encountered constructing the request, if any.
func (r *Request) Error() error {
return r.err
}

// URL returns the current working URL. Check the result of Error() to ensure
// that the returned URL is valid.
func (r *Request) URL() *url.URL {
p := r.pathPrefix
if r.namespaceSet && len(r.namespace) > 0 {
Expand Down
20 changes: 20 additions & 0 deletions rest/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,26 @@ func TestRequestVersionedParamsFromListOptions(t *testing.T) {
}
}

func TestRequestVersionedParamsWithInvalidScheme(t *testing.T) {
parameterCodec := runtime.NewParameterCodec(runtime.NewScheme())
r := (&Request{c: &RESTClient{content: ClientContentConfig{GroupVersion: v1.SchemeGroupVersion}}})
r.VersionedParams(&v1.PodExecOptions{Stdin: false, Stdout: true},
parameterCodec)

if r.Error() == nil {
t.Errorf("should have recorded an error: %#v", r.params)
}
}

func TestRequestError(t *testing.T) {
// Invalid body, see TestRequestBody()
r := (&Request{}).Body([]string{"test"})

if r.Error() != r.err {
t.Errorf("getter should be identical to reference: %#v %#v", r.Error(), r.err)
}
}

func TestRequestURI(t *testing.T) {
r := (&Request{}).Param("foo", "a")
r.Prefix("other")
Expand Down

0 comments on commit 22e2a9b

Please sign in to comment.