Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
dbda3f73
Commit
dbda3f73
authored
Apr 08, 2015
by
deads2k
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make .Stream handle error status codes
parent
24b478dd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
0 deletions
+42
-0
request.go
pkg/client/request.go
+26
-0
request_test.go
pkg/client/request_test.go
+16
-0
No files found.
pkg/client/request.go
View file @
dbda3f73
...
@@ -540,6 +540,8 @@ func isProbableEOF(err error) bool {
...
@@ -540,6 +540,8 @@ func isProbableEOF(err error) bool {
// Stream formats and executes the request, and offers streaming of the response.
// Stream formats and executes the request, and offers streaming of the response.
// Returns io.ReadCloser which could be used for streaming of the response, or an error
// Returns io.ReadCloser which could be used for streaming of the response, or an error
// Any non-2xx http status code causes an error. If we get a non-2xx code, we try to convert the body into an APIStatus object.
// If we can, we return that as an error. Otherwise, we create an error that lists the http status and the content of the response.
func
(
r
*
Request
)
Stream
()
(
io
.
ReadCloser
,
error
)
{
func
(
r
*
Request
)
Stream
()
(
io
.
ReadCloser
,
error
)
{
if
r
.
err
!=
nil
{
if
r
.
err
!=
nil
{
return
nil
,
r
.
err
return
nil
,
r
.
err
...
@@ -556,6 +558,30 @@ func (r *Request) Stream() (io.ReadCloser, error) {
...
@@ -556,6 +558,30 @@ func (r *Request) Stream() (io.ReadCloser, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
switch
{
case
(
resp
.
StatusCode
>=
200
)
&&
(
resp
.
StatusCode
<
300
)
:
return
resp
.
Body
,
nil
default
:
// we have a decent shot at taking the object returned, parsing it as a status object and returning a more normal error
bodyBytes
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"%v while accessing %v"
,
resp
.
Status
,
r
.
finalURL
())
}
if
runtimeObject
,
err
:=
r
.
codec
.
Decode
(
bodyBytes
);
err
==
nil
{
statusError
:=
errors
.
FromObject
(
runtimeObject
)
if
_
,
ok
:=
statusError
.
(
APIStatus
);
ok
{
return
nil
,
statusError
}
}
bodyText
:=
string
(
bodyBytes
)
return
nil
,
fmt
.
Errorf
(
"%s while accessing %v: %s"
,
resp
.
Status
,
r
.
finalURL
(),
bodyText
)
}
return
resp
.
Body
,
nil
return
resp
.
Body
,
nil
}
}
...
...
pkg/client/request_test.go
View file @
dbda3f73
...
@@ -531,6 +531,22 @@ func TestRequestStream(t *testing.T) {
...
@@ -531,6 +531,22 @@ func TestRequestStream(t *testing.T) {
},
},
Err
:
true
,
Err
:
true
,
},
},
{
Request
:
&
Request
{
client
:
clientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
return
&
http
.
Response
{
StatusCode
:
http
.
StatusUnauthorized
,
Body
:
ioutil
.
NopCloser
(
bytes
.
NewReader
([]
byte
(
runtime
.
EncodeOrDie
(
testapi
.
Codec
(),
&
api
.
Status
{
Status
:
api
.
StatusFailure
,
Reason
:
api
.
StatusReasonUnauthorized
,
})))),
},
nil
}),
codec
:
latest
.
Codec
,
baseURL
:
&
url
.
URL
{},
},
Err
:
true
,
},
}
}
for
i
,
testCase
:=
range
testCases
{
for
i
,
testCase
:=
range
testCases
{
body
,
err
:=
testCase
.
Request
.
Stream
()
body
,
err
:=
testCase
.
Request
.
Stream
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment