-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Callback with error if error thrown due to network #605
Conversation
Codecov Report
@@ 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.
|
@@ -55,7 +55,10 @@ export default class Client { | |||
}; | |||
return callback(error); | |||
}) | |||
.catch(() => { | |||
.catch(err => { | |||
if (err) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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;
})
6ee76a1
to
6d518c5
Compare
35e1758
to
dc14b57
Compare
The Fetch API throws TyperError if there are network issues
dc14b57
to
f5cb093
Compare
Hi @mlabieniec , what are your thoughts about the fix? |
👍 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 ... |
// If error happens here, the request failed | ||
// if it is TypeError throw network error | ||
if (err instanceof TypeError) { | ||
throw new Error('Network error'); |
There was a problem hiding this comment.
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 {
There was a problem hiding this comment.
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.
When will this fix be live on npm? |
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 |
The Fetch API throws TyperError if there are network issues. fixes #558 #389