fetch-intercept icon indicating copy to clipboard operation
fetch-intercept copied to clipboard

Cannot read property 'apply' of undefined

Open MartinDawson opened this issue 8 years ago • 4 comments

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);
	    }
	  });

MartinDawson avatar Jul 23 '17 11:07 MartinDawson

Same issue here.

TheDutchCoder avatar Apr 17 '18 20:04 TheDutchCoder

Seems like the check for request comes to late. Should be an easy fix. Pull requests are welcome.

mlegenhausen avatar Apr 18 '18 06:04 mlegenhausen

Temporary workaround should be defining request function at least as empty:

{
  request: () => {},
  requestError: yourRequestErrorFunction,
}

RusinovAnton avatar Jul 06 '18 15:07 RusinovAnton

Looked at the tests and figured out that requestError is pointless without request interceptor because requestError would only catch errors from interceptors?

RusinovAnton avatar Jul 06 '18 15:07 RusinovAnton