From 171b2187bfac0ad4668038ea25ab1fb6df2b59a8 Mon Sep 17 00:00:00 2001 From: veera Date: Thu, 5 Apr 2018 19:07:37 -0700 Subject: [PATCH 1/6] Callback with error if error thrown due to network The Fetch API throws TyperError if there are network issues --- packages/amazon-cognito-identity-js/src/Client.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/amazon-cognito-identity-js/src/Client.js b/packages/amazon-cognito-identity-js/src/Client.js index 1a5e41bbd4a..34fe5651622 100644 --- a/packages/amazon-cognito-identity-js/src/Client.js +++ b/packages/amazon-cognito-identity-js/src/Client.js @@ -55,7 +55,10 @@ export default class Client { }; return callback(error); }) - .catch(() => { + .catch((err) => { + if(err){ + return callback(err); + } // Taken from aws-sdk-js/lib/protocol/json.js const code = (response.headers.get('x-amzn-errortype') || 'UnknownError').split(':')[0]; const error = { From 126664e2115039d102450c3adb5bc3a2a72a69da Mon Sep 17 00:00:00 2001 From: veera Date: Thu, 5 Apr 2018 19:13:04 -0700 Subject: [PATCH 2/6] remove unnecessary brackets --- packages/amazon-cognito-identity-js/src/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/amazon-cognito-identity-js/src/Client.js b/packages/amazon-cognito-identity-js/src/Client.js index 34fe5651622..df9f7a8f14a 100644 --- a/packages/amazon-cognito-identity-js/src/Client.js +++ b/packages/amazon-cognito-identity-js/src/Client.js @@ -55,7 +55,7 @@ export default class Client { }; return callback(error); }) - .catch((err) => { + .catch(err => { if(err){ return callback(err); } From ee4addd36135ed1ce7b3d61c58b3b7e7a4161188 Mon Sep 17 00:00:00 2001 From: veera Date: Thu, 5 Apr 2018 19:27:05 -0700 Subject: [PATCH 3/6] fix spacing --- packages/amazon-cognito-identity-js/src/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/amazon-cognito-identity-js/src/Client.js b/packages/amazon-cognito-identity-js/src/Client.js index df9f7a8f14a..6f512118637 100644 --- a/packages/amazon-cognito-identity-js/src/Client.js +++ b/packages/amazon-cognito-identity-js/src/Client.js @@ -56,7 +56,7 @@ export default class Client { return callback(error); }) .catch(err => { - if(err){ + if (err) { return callback(err); } // Taken from aws-sdk-js/lib/protocol/json.js From f2ab70611faaed5aed57f0f42e69fa06ae762b87 Mon Sep 17 00:00:00 2001 From: veera Date: Fri, 6 Apr 2018 11:57:05 -0700 Subject: [PATCH 4/6] handle fetch request failure and throw error /w message 'Network error' --- .../amazon-cognito-identity-js/src/Client.js | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/packages/amazon-cognito-identity-js/src/Client.js b/packages/amazon-cognito-identity-js/src/Client.js index 6f512118637..200ccde4e92 100644 --- a/packages/amazon-cognito-identity-js/src/Client.js +++ b/packages/amazon-cognito-identity-js/src/Client.js @@ -40,6 +40,13 @@ export default class Client { .then(resp => { response = resp; return resp; + }, err => { + // If error happens here, the request failed + // if it is TypeError throw network error + if (err instanceof TypeError) { + throw new Error('Network error'); + } + throw err; }) .then(resp => resp.json().catch(() => ({}))) .then(data => { @@ -56,17 +63,32 @@ export default class Client { return callback(error); }) .catch(err => { - if (err) { - return callback(err); + let code = 'UnknownError', error = {}; + + // first check if we have a service error + if ( response && response.headers && response.headers.get('x-amzn-errortype') ) { + try { + code = (response.headers.get('x-amz-errortype')).split(':')[0]; + error = { + code, + name: code, + statusCode: response.status, + message: (response.status) ? response.status.toString() : null, + }; + } catch( error ) { + // pass through so it doesn't get swallowed if we can't parse it + } + // otherwise check error + } else if ( err instanceof Error ) { + error = { + code: err.name, + name: err.name, + message: err.message + }; + // finally case will return 'UnknownError' + } else { + error = { code }; } - // Taken from aws-sdk-js/lib/protocol/json.js - const code = (response.headers.get('x-amzn-errortype') || 'UnknownError').split(':')[0]; - const error = { - code, - name: code, - statusCode: response.status, - message: response.status.toString(), - }; return callback(error); }); } From 724967c3937c25d0dafcce677d5e7ba8a0d2949e Mon Sep 17 00:00:00 2001 From: veera Date: Fri, 6 Apr 2018 14:53:04 -0700 Subject: [PATCH 5/6] return Unknown error for anything other than Network error. Some linting --- .../amazon-cognito-identity-js/src/Client.js | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/packages/amazon-cognito-identity-js/src/Client.js b/packages/amazon-cognito-identity-js/src/Client.js index 200ccde4e92..ccfcec27ccf 100644 --- a/packages/amazon-cognito-identity-js/src/Client.js +++ b/packages/amazon-cognito-identity-js/src/Client.js @@ -23,7 +23,7 @@ export default class Client { const headers = { 'Content-Type': 'application/x-amz-json-1.1', 'X-Amz-Target': `AWSCognitoIdentityProviderService.${operation}`, - 'X-Amz-User-Agent': this.userAgent + 'X-Amz-User-Agent': this.userAgent, }; const options = { @@ -63,31 +63,29 @@ export default class Client { return callback(error); }) .catch(err => { - let code = 'UnknownError', error = {}; + let error = { code: 'UnknownError', message: 'Unkown error' }; // first check if we have a service error - if ( response && response.headers && response.headers.get('x-amzn-errortype') ) { + if (response && response.headers && response.headers.get('x-amzn-errortype')) { try { - code = (response.headers.get('x-amz-errortype')).split(':')[0]; - error = { - code, - name: code, - statusCode: response.status, - message: (response.status) ? response.status.toString() : null, - }; - } catch( error ) { - // pass through so it doesn't get swallowed if we can't parse it - } - // otherwise check error - } else if ( err instanceof Error ) { + const code = (response.headers.get('x-amz-errortype')).split(':')[0]; error = { - code: err.name, - name: err.name, - message: err.message + code, + name: code, + statusCode: response.status, + message: (response.status) ? response.status.toString() : null, }; + } catch (ex) { + // pass through so it doesn't get swallowed if we can't parse it + } + // otherwise check if error is Network error + } else if (err instanceof Error && err.message === 'Network error') { + error = { + code: err.name, + name: err.name, + message: err.message, + }; // finally case will return 'UnknownError' - } else { - error = { code }; } return callback(error); }); From f5cb093ac1677c6c7a520140108ab55875607841 Mon Sep 17 00:00:00 2001 From: veera Date: Fri, 6 Apr 2018 17:48:05 -0700 Subject: [PATCH 6/6] comments --- packages/amazon-cognito-identity-js/src/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/amazon-cognito-identity-js/src/Client.js b/packages/amazon-cognito-identity-js/src/Client.js index ccfcec27ccf..1a941a8649f 100644 --- a/packages/amazon-cognito-identity-js/src/Client.js +++ b/packages/amazon-cognito-identity-js/src/Client.js @@ -63,6 +63,7 @@ export default class Client { return callback(error); }) .catch(err => { + // default to return 'UnknownError' let error = { code: 'UnknownError', message: 'Unkown error' }; // first check if we have a service error @@ -85,7 +86,6 @@ export default class Client { name: err.name, message: err.message, }; - // finally case will return 'UnknownError' } return callback(error); });