Skip to content

Handle unsuccessful fetch#157

Merged
Gudahtt merged 2 commits intodevelopfrom
handle-unsuccessful-fetch
Oct 3, 2019
Merged

Handle unsuccessful fetch#157
Gudahtt merged 2 commits intodevelopfrom
handle-unsuccessful-fetch

Conversation

@Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Sep 20, 2019

The fetch API will throw an exception if a network error is encountered during a fetch invocation, but an unsuccessful response will not. We should never assume that the response from fetch is
successful without checking first. We can't even assume that successfully calling .json() on a request implies that it succeeded, because failed responses can pass information in the response body as well.

A successfulFetch utility method has been added that verifies the fetch response was successful, and throws an error if it was not. This method is now used for the other two fetch utility methods, and each instance of fetch has been updated to use one of these methods.

New tests have been added for successfulFetch. fetch-mock was updated to make it easier to specify the status of responses from calls to the mock fetch instance.

Also, 'isomorphic-fetch' has been imported in the index. This ensures that 'isomorphic-fetch' is imported before anything else.

Previously 'isomorphic-fetch' was imported at the beginning of each separate module that used 'fetch', which was needlessly verbose and easy to forget to do (it was missing from one module already).

This would normally break the tests, as the tests don't import modules via the main index file. This was resolved by adding a setupTests file that imports isomorphic-fetch before running any tests.

This ensures that 'isomorphic-fetch' is imported before anything else.

Previously 'isomorphic-fetch' was imported at the beginning of each
separate module that used 'fetch', which was needlessly verbose and
easy to forget to do (it was missing from one module already).

This would normally break the tests, as the tests don't import modules
via the main index file. This was resolved by adding a `setupTests`
file that imports `isomorphic-fetch` before running any tests.
The `fetch` API will throw an exception if a network error is
encountered during a `fetch` invocation, but an unsuccessful response
will not. We should never assume that the response from `fetch` is
successful without checking first. We can't even assume that
successfully calling `.json()` on a request implies that it succeeded,
because failed responses can pass information in the response body as
well.

A `successfulFetch` utility method has been added that verifies the
fetch response was successful, and throws an error if it was not.
This method is now used for the other two `fetch` utility methods, and
each instance of `fetch` has been updated to use one of these methods.

New tests have been added for `successfulFetch`. `fetch-mock` was
updated to make it easier to specify the status of responses from calls
to the mock fetch instance.
@codecov-io
Copy link

codecov-io commented Sep 20, 2019

Codecov Report

Merging #157 into develop will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff           @@
##           develop   #157   +/-   ##
======================================
  Coverage      100%   100%           
======================================
  Files           22     22           
  Lines         1484   1472   -12     
  Branches       206    207    +1     
======================================
- Hits          1484   1472   -12
Impacted Files Coverage Δ
src/assets/AssetsDetectionController.ts 100% <ø> (ø) ⬆️
src/keyring/KeyringController.ts 100% <ø> (ø) ⬆️
src/assets/TokenBalancesController.ts 100% <ø> (ø) ⬆️
src/assets/AssetsController.ts 100% <ø> (ø) ⬆️
src/assets/AssetsContractController.ts 100% <ø> (ø) ⬆️
src/assets/CurrencyRateController.ts 100% <100%> (ø) ⬆️
src/assets/TokenRatesController.ts 100% <100%> (ø) ⬆️
src/network/NetworkStatusController.ts 100% <100%> (ø) ⬆️
src/third-party/ShapeShiftController.ts 100% <100%> (ø) ⬆️
src/util.ts 100% <100%> (ø) ⬆️
... and 2 more

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 b054cdc...910b1ad. Read the comment docs.

Copy link
Contributor

@danfinlay danfinlay left a comment

Choose a reason for hiding this comment

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

This looks good. Great that mobile will benefit from these improvements!

Copy link
Contributor

@whymarrh whymarrh left a comment

Choose a reason for hiding this comment

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

LGTM

@Gudahtt Gudahtt merged commit 91ba737 into develop Oct 3, 2019
@whymarrh whymarrh deleted the handle-unsuccessful-fetch branch October 4, 2019 16:20
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
* Add 'isomorphic-fetch' import to index

This ensures that 'isomorphic-fetch' is imported before anything else.

Previously 'isomorphic-fetch' was imported at the beginning of each
separate module that used 'fetch', which was needlessly verbose and
easy to forget to do (it was missing from one module already).

This would normally break the tests, as the tests don't import modules
via the main index file. This was resolved by adding a `setupTests`
file that imports `isomorphic-fetch` before running any tests.

* Handle unsuccessful fetch attempts

The `fetch` API will throw an exception if a network error is
encountered during a `fetch` invocation, but an unsuccessful response
will not. We should never assume that the response from `fetch` is
successful without checking first. We can't even assume that
successfully calling `.json()` on a request implies that it succeeded,
because failed responses can pass information in the response body as
well.

A `successfulFetch` utility method has been added that verifies the
fetch response was successful, and throws an error if it was not.
This method is now used for the other two `fetch` utility methods, and
each instance of `fetch` has been updated to use one of these methods.

New tests have been added for `successfulFetch`. `fetch-mock` was
updated to make it easier to specify the status of responses from calls
to the mock fetch instance.
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
* Add 'isomorphic-fetch' import to index

This ensures that 'isomorphic-fetch' is imported before anything else.

Previously 'isomorphic-fetch' was imported at the beginning of each
separate module that used 'fetch', which was needlessly verbose and
easy to forget to do (it was missing from one module already).

This would normally break the tests, as the tests don't import modules
via the main index file. This was resolved by adding a `setupTests`
file that imports `isomorphic-fetch` before running any tests.

* Handle unsuccessful fetch attempts

The `fetch` API will throw an exception if a network error is
encountered during a `fetch` invocation, but an unsuccessful response
will not. We should never assume that the response from `fetch` is
successful without checking first. We can't even assume that
successfully calling `.json()` on a request implies that it succeeded,
because failed responses can pass information in the response body as
well.

A `successfulFetch` utility method has been added that verifies the
fetch response was successful, and throws an error if it was not.
This method is now used for the other two `fetch` utility methods, and
each instance of `fetch` has been updated to use one of these methods.

New tests have been added for `successfulFetch`. `fetch-mock` was
updated to make it easier to specify the status of responses from calls
to the mock fetch instance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants