FetchThrow.js is a mixin for the Fetch API for browsers and Node.js that rejects the returned promise with a FetchError.js instance should the network request fail or the response have a status of at least 400 and less than 600 (as defined for error responses in the HTTP specs).
npm install fetch-throw
FetchThrow.js follows semantic versioning, so feel free to depend on its major version with something like
>= 1.0.0 < 2 (a.k.a
If you'd like more convenient access to the thrown
FetchError instance (e.g. for
instanceof checks), also install
fetch-error and ensure there's only one installed:
npm install fetch-errornpm dedupe fetch-error
Wrap the native
fetch function with the one from FetchThrow.js:
var fetch = globalfetch
Then call the returned function as you would with the regular Fetch API:
var res =
/models respond with 404,
res will be rejected with an instance of
FetchError property on
FetchThrow is identical to doing
var FetchError = FetchError res
In case of unexpected errors (such as network name lookup failures) thrown by
fetch before a request is made,
res will also be rejected with an instance of
FetchError. This allows you to identify failed requests for logging, for example, as shown above.
For a description of properties on the error, see below.
Browsers have the Fetch API available at
var fetch = windowfetch
var fetch =
Properties on an instance of FetchError
0 in case of network error.
|A new Fetch API
Request object to help identify the request.
|The Fetch API
Response of the failed request.
|In case of non-response errors, it's set to the original error.
FetchThrow.js is released under a Lesser GNU Affero General Public License, which in summary means:
- You can use this program for no cost.
- You can use this program for both personal and commercial reasons.
- You do not have to share your own program's code which uses this program.
- You have to share modifications (e.g. bug-fixes) you've made to this program.
For more convoluted language, see the