fetch-intercept
fetch-intercept copied to clipboard
Cannot read property 'apply' of undefined
This happens when I specify the requestError function. Tested it in a new create-react-app with https://github.com/github/fetch.
import fetchIntercept from 'fetch-intercept';
const unregister = fetchIntercept.register({
requestError: (error) => {
return Promise.reject(error);
},
responseError: (error) => {
// catches bad error here
return Promise.reject(error);
},
});
fetch('http://google.com');
// Unregister your interceptor
unregister();
Looking at the source, _ref.request is undefined.
reversedInterceptors.forEach(function (_ref) {
var request = _ref.request; // request is undefined
var requestError = _ref.requestError;
if (request || requestError) {
promise = promise.then(function (args) {
return request.apply(undefined, _toConsumableArray(args));
}, requestError);
}
});
Same issue here.
Seems like the check for request comes to late. Should be an easy fix. Pull requests are welcome.
Temporary workaround should be defining request function at least as empty:
{
request: () => {},
requestError: yourRequestErrorFunction,
}
Looked at the tests and figured out that requestError is pointless without request interceptor because requestError would only catch errors from interceptors?