Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback with error if error thrown due to network #605

Merged
merged 9 commits into from Apr 19, 2018

Conversation

cheedep
Copy link
Contributor

@cheedep cheedep commented Apr 6, 2018

The Fetch API throws TyperError if there are network issues. fixes #558 #389

@codecov-io
Copy link

@codecov-io codecov-io commented Apr 6, 2018

Codecov Report

Merging #605 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #605   +/-   ##
=======================================
  Coverage   87.28%   87.28%           
=======================================
  Files          72       72           
  Lines        3428     3428           
  Branches      652      652           
=======================================
  Hits         2992     2992           
  Misses        419      419           
  Partials       17       17

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 16d1a1a...26e0f08. Read the comment docs.

@@ -55,7 +55,10 @@ export default class Client {
};
return callback(error);
})
.catch(() => {
.catch(err => {
if (err) {
Copy link
Contributor

@mlabieniec mlabieniec Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check here if error is actually a TypeError?

if (err && err instanceof TypeError)

for example: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Copy link
Contributor Author

@cheedep cheedep Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would make sense if a the callback is called with a new error with a more meaningful message. but since most clients possibly handle the same error when directly using the fetch, I thought this is better.

Copy link
Contributor Author

@cheedep cheedep Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. Should we also be checking for the error message?

Copy link
Contributor

@mlabieniec mlabieniec Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe so we have a uniform approach and 1 return we could do something like:

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 TypeError ) {
    error = {
        code: err.name,
        name: err.name,
        message: err.message
    };
// finally case will return 'UnknownError'
} else {
    error = { code };
}
return callback(error);

Copy link
Contributor Author

@cheedep cheedep Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is much better. How about providing onRejected handler to the then and throwing an error with a better message. Example:

fetch(this.endpoint, options)
      .then(resp => {
        response = resp;
        return resp;
      }, err => {
        // If error happens here, the request failed and if it is type error throw network error
        if (err instanceof TypeError) {
          throw new Error('Network error');
        }
        throw err;
      })

@cheedep cheedep force-pushed the bug/cognito-js-network-errors branch from 6ee76a1 to 6d518c5 Compare Apr 6, 2018
@cheedep cheedep closed this Apr 6, 2018
@cheedep cheedep reopened this Apr 6, 2018
@cheedep cheedep force-pushed the bug/cognito-js-network-errors branch 2 times, most recently from 35e1758 to dc14b57 Compare Apr 7, 2018
@cheedep cheedep force-pushed the bug/cognito-js-network-errors branch from dc14b57 to f5cb093 Compare Apr 7, 2018
@cheedep
Copy link
Contributor Author

@cheedep cheedep commented Apr 11, 2018

Hi @mlabieniec , what are your thoughts about the fix?

@lukezbikowski
Copy link

@lukezbikowski lukezbikowski commented Apr 18, 2018

👍 Any chance to merge it soon please? :-) This issue is very problematic (especially for mobile apps). There is no way to catch that error (unless I missed something)... the only workarround we have is downgrade of amazon-cognito-identity-js ...

@mlabieniec mlabieniec added this to Backlog in amazon-cognito-identity-js via automation Apr 19, 2018
@mlabieniec mlabieniec added Auth Cognito labels Apr 19, 2018
// If error happens here, the request failed
// if it is TypeError throw network error
if (err instanceof TypeError) {
throw new Error('Network error');
Copy link
Contributor

@mlabieniec mlabieniec Apr 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably return:
return throw new Error('Network error');

or run an } else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing an error automatically returns control to the caller. You dont need return or a branch here.

@mlabieniec mlabieniec merged commit 3e1e97d into aws-amplify:master Apr 19, 2018
1 check passed
amazon-cognito-identity-js automation moved this from Backlog to Done Apr 19, 2018
@stripathix
Copy link

@stripathix stripathix commented May 4, 2018

When will this fix be live on npm?

@github-actions
Copy link

@github-actions github-actions bot commented Jun 20, 2021

This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Auth Cognito
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

6 participants