File tree 2 files changed +9
-9
lines changed
2 files changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -41,16 +41,15 @@ async function parseResponse<T>(resp: Response): Promise<T> {
41
41
let json
42
42
43
43
try {
44
- // An HTTP 204 - No Content response doesn't contain a body so trying to call .json() on it would throw
45
- json = resp . status === 204 ? { } : await resp . json ( )
44
+ json = await resp . json ( )
46
45
} catch {
47
- if ( resp . headers && resp . headers . get ( 'content-length' ) !== '0' ) {
48
- throw new Error ( `Invalid response content: ${ resp . statusText } ` )
49
- }
46
+ json = { }
50
47
}
51
48
52
49
if ( ! resp . ok ) {
53
- const errTxt = isErrorResponse ( json ) ? `${ json . code } : ${ json . message } ` : resp . statusText
50
+ const errTxt = isErrorResponse ( json )
51
+ ? `CGW error - ${ json . code } : ${ json . message } `
52
+ : `CGW error - status ${ resp . statusText } `
54
53
throw new Error ( errTxt )
55
54
}
56
55
Original file line number Diff line number Diff line change @@ -198,18 +198,19 @@ describe('utils', () => {
198
198
} )
199
199
} )
200
200
201
- it ( 'should not throw for a 204 response' , async ( ) => {
202
- const jsonMock = jest . fn ( )
201
+ it ( 'should not throw for an non-JSON response' , async ( ) => {
202
+ const jsonMock = jest . fn ( ) . mockRejectedValue ( 'error' )
203
+
203
204
fetchMock . mockImplementation ( ( ) => {
204
205
return Promise . resolve ( {
205
206
ok : true ,
206
207
status : 204 ,
208
+ statusText : 'No Content' ,
207
209
json : jsonMock ,
208
210
} )
209
211
} )
210
212
211
213
await expect ( fetchData ( '/test/safe' , 'DELETE' ) ) . resolves . toEqual ( { } )
212
- expect ( jsonMock ) . not . toHaveBeenCalled ( )
213
214
214
215
expect ( fetch ) . toHaveBeenCalledWith ( '/test/safe' , {
215
216
method : 'DELETE' ,
You can’t perform that action at this time.
0 commit comments