mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +00:00
client-go: add an Error() function on Request
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>
This commit is contained in:
parent
70dde8d746
commit
f69c1c4746
@ -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 {
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user