Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

docs($http): Interceptors, example unclear / conflicts with description #7431

Closed
fwielstra opened this issue May 12, 2014 · 1 comment
Closed

Comments

@fwielstra
Copy link
Contributor

I was reviewing some of our application code and came across a branch not covered by unit tests in a Request interceptor:

return {
  request: function requestIntercepor(config) {
    _.extend(config.headers, {
      'X-Requested-By': 'my-app'
    });
    return config || $q.when(config);
  }
};

This failed when config was undefined, so I dove into the docs to see if config is ever undefined: https://docs.angularjs.org/api/ng/service/$http#interceptors. The docs in this case state:

"request: interceptors get called with http config object. The function is free to modify the config or create a new one. The function needs to return the config directly or as a promise."

and the example:

'request': function(config) {
    // do something on success
    return config || $q.when(config);
  },

Now, one or the other of these are incomplete or incorrect: either the example is a literal translation of the documentation (return config or a promise, but don't use this actual code), or the documentation is incomplete and needs a sentence stating that if config is a falsy value, the method should return this value wrapped in a resolving promise (or something to that effect).

Does anyone have more inside knowledge about $http and how the request interceptor can be called to tweak this bit of documentation? I just want to know if I need to add a test case (and update the implementation to deal with falsy values) or if I can remove the or. Haven't run into a 'config is undefined' error so far myself, so I'm leaning towards a code / example fix.

@Narretz
Copy link
Contributor

Narretz commented May 12, 2014

In the request interceptor, the config as an argument can never be undefined, it is always a config object (Since you must pass an object to $http with at least the url defined). Likewise, the return value must always be a config object, but you have the choice to either return it directly or return a promise that resolves to a config object. I guess the documentation should be be updated to make this more clear (the comment is also irrelevant, I think).

IgorMinar pushed a commit that referenced this issue May 21, 2014
The documentation and code example of $http interceptors is unclear about whether config can be null
or not, and whether the result should always be a promise or not. This pr clears up the documentation
a bit and removes the literal 'or a promise' interpretation of the docs in the code example.

Closes #7431
Closes #7460
RichardLitt pushed a commit to RichardLitt/angular.js that referenced this issue May 24, 2014
The documentation and code example of $http interceptors is unclear about whether config can be null
or not, and whether the result should always be a promise or not. This pr clears up the documentation
a bit and removes the literal 'or a promise' interpretation of the docs in the code example.

Closes angular#7431
Closes angular#7460
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants