Skip to content

Commit 203d50b

Browse files
committed
fix(amazon-cognito-identity-js): attach raw message for unknown errors
1 parent 69be072 commit 203d50b

File tree

1 file changed

+22
-4
lines changed
  • packages/amazon-cognito-identity-js/src

1 file changed

+22
-4
lines changed

packages/amazon-cognito-identity-js/src/Client.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,39 @@ export default class Client {
3535
};
3636

3737
let response;
38+
let responseJsonData;
3839

3940
fetch(this.endpoint, options)
4041
.then(resp => {
4142
response = resp;
4243
return resp;
43-
}, err => {
44+
})
45+
.catch(err => {
4446
// If error happens here, the request failed
4547
// if it is TypeError throw network error
4648
if (err instanceof TypeError) {
4749
throw new Error('Network error');
4850
}
4951
throw err;
5052
})
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+
})
5267
.then(data => {
68+
// return parsed body stream
5369
if (response.ok) return callback(null, data);
70+
responseJsonData = data;
5471

5572
// Taken from aws-sdk-js/lib/protocol/json.js
5673
// eslint-disable-next-line no-underscore-dangle
@@ -63,8 +80,9 @@ export default class Client {
6380
return callback(error);
6481
})
6582
.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};
6886

6987
// first check if we have a service error
7088
if (response && response.headers && response.headers.get('x-amzn-errortype')) {

0 commit comments

Comments
 (0)