@@ -35,22 +35,39 @@ export default class Client {
35
35
} ;
36
36
37
37
let response ;
38
+ let responseJsonData ;
38
39
39
40
fetch ( this . endpoint , options )
40
41
. then ( resp => {
41
42
response = resp ;
42
43
return resp ;
43
- } , err => {
44
+ } )
45
+ . catch ( err => {
44
46
// If error happens here, the request failed
45
47
// if it is TypeError throw network error
46
48
if ( err instanceof TypeError ) {
47
49
throw new Error ( 'Network error' ) ;
48
50
}
49
51
throw err ;
50
52
} )
51
- . then ( resp => resp . json ( ) . catch ( ( ) => ( { } ) ) )
53
+ . then ( resp => resp . json ( ) )
54
+ . catch ( err => {
55
+ // If error happens here
56
+ // cannot parse the body stream, return undefined
57
+ if ( response . ok ) return callback ( null , undefined ) ;
58
+ else {
59
+ const error = {
60
+ code : response . status ,
61
+ statusCode : response . status ,
62
+ message : response . statusText
63
+ }
64
+ throw error ;
65
+ }
66
+ } )
52
67
. then ( data => {
68
+ // return parsed body stream
53
69
if ( response . ok ) return callback ( null , data ) ;
70
+ responseJsonData = data ;
54
71
55
72
// Taken from aws-sdk-js/lib/protocol/json.js
56
73
// eslint-disable-next-line no-underscore-dangle
@@ -63,8 +80,9 @@ export default class Client {
63
80
return callback ( error ) ;
64
81
} )
65
82
. catch ( err => {
66
- // default to return 'UnknownError'
67
- let error = { code : 'UnknownError' , message : 'Unknown error: ' + err } ;
83
+ // if cannot split the data
84
+ // default to return 'UnknownError' with the json data from response
85
+ let error = { code : 'UnknownError' , message : 'Unknown error, the response body from fetch is: ' + responseJsonData } ;
68
86
69
87
// first check if we have a service error
70
88
if ( response && response . headers && response . headers . get ( 'x-amzn-errortype' ) ) {
0 commit comments