File tree 2 files changed +37
-1
lines changed
2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -153,7 +153,17 @@ export default function fetchWrapper(
153
153
async function getResponseData ( response : Response ) {
154
154
const contentType = response . headers . get ( "content-type" ) ;
155
155
if ( / a p p l i c a t i o n \/ j s o n / . test ( contentType ! ) ) {
156
- return response . json ( ) ;
156
+ return (
157
+ response
158
+ . json ( )
159
+ // In the event that we get an empty response body we fallback to
160
+ // using .text(), but this should be investigated since if this were
161
+ // to occur in the GitHub API it really should not return an empty body.
162
+ . catch ( ( ) => response . text ( ) )
163
+ // `node-fetch` is throwing a "body used already for" error if `.text()` is run
164
+ // after a failed .json(). To account for that we fallback to an empty string
165
+ . catch ( ( ) => "" )
166
+ ) ;
157
167
}
158
168
159
169
if ( ! contentType || / ^ t e x t \/ | c h a r s e t = u t f - 8 $ / . test ( contentType ) ) {
Original file line number Diff line number Diff line change @@ -400,6 +400,32 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
400
400
) ;
401
401
} ) ;
402
402
403
+ it ( "error response with no body (octokit/request.js#649)" , ( ) => {
404
+ const mock = fetchMock
405
+ . sandbox ( )
406
+ . get ( "path:/repos/octokit-fixture-org/hello-world/contents/README.md" , {
407
+ status : 500 ,
408
+ body : undefined ,
409
+ headers : {
410
+ "content-type" : "application/json" ,
411
+ } ,
412
+ } ) ;
413
+
414
+ return request ( "GET /repos/{owner}/{repo}/contents/{path}" , {
415
+ headers : {
416
+ accept : "content-type: application/json" ,
417
+ } ,
418
+ owner : "octokit-fixture-org" ,
419
+ repo : "hello-world" ,
420
+ path : "README.md" ,
421
+ request : {
422
+ fetch : mock ,
423
+ } ,
424
+ } ) . catch ( ( error ) => {
425
+ expect ( error . response . data ) . toEqual ( "" ) ;
426
+ } ) ;
427
+ } ) ;
428
+
403
429
it ( "non-JSON response" , ( ) => {
404
430
const mock = fetchMock
405
431
. sandbox ( )
You can’t perform that action at this time.
0 commit comments