angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

ngResource interceptors called inconsistently

Open jrm-airwatch opened this issue 12 years ago • 7 comments

Action-specific interceptors defined in a $resource call are called inconsistently. A response interceptor is called before the success callback, but a responseError interceptor is called after the error callback. Further, the success callback is called even if the response interceptor rejects, and the error callback is called even if the responseError interceptor resolves. And response data is populated before any interceptors run.

This behavior runs contrary to the expectation that an interceptor should transparently intercept results before passing them to the consumer. A more sensible flow would be

http success -> interceptor.response (resolves) -> populate results -> success callback http success -> interceptor.response (rejects) -> error callback http error -> interceptor.responseError (resolves) -> populate results -> success callback http error -> interceptor.responseError (rejects) -> error callback

As it stands, even if a responseError interceptor successfully retries, the error callback is still called, and no response data is populated!

plunker

jrm-airwatch avatar Sep 29 '14 23:09 jrm-airwatch

This is quite a frustrating issue. If it helps, here is a plunker that demonstrates the responseError issues. Check the console logs to see the issue.

Note that:

  • interceptors are called after callbacks
  • modified payloads (both content and success/rejection) are ignored

nicole-ashley avatar Feb 12 '16 04:02 nicole-ashley

Indeed the concept of interceptors in the context of $resource is very vague. In the documentation, it is implied that interceptors are essentially $http interceptors, but they are used quite differently. Furthermore, their behavior is inconsistent and not well defined.

Similar issues have been discussed in https://github.com/angular/angular.js/issues/11409, as well. IMO, $resource interceptors should be behave like this:

  • They should be applied to the promise returned by the call to $http (before anything $resource-specific).
  • The appropriate $resource interceptor should be applied (if specified by the user). Interceptors should be passed the response - nothing $resource-specific yet.
  • If the interceptor rejects, the error callback should be called (and the data should not be updated).
  • If the interceptor resolves, $resource should update the returned instance based on the content of response.data and call the success callback.

Thoughts anyone ?

gkalpak avatar Feb 17 '16 22:02 gkalpak

I overall agree with the original issue description and @gkalpak's assessment. Also, I'm making the assumption that transformRequest and transformResponse both execute before/after the interceptors are considered, so I'm deliberately not including them.

Errors on response interceptors aren't the only problem - request interceptors are not actually supported by $resource at this time (see my patch), and introducing them makes the resolution of request interceptor errors tricky. If an $http request interceptor errors, which handler in $resource should get it? Since you've already passed down to the $http level, I can argue that the request has already been handed off, and everything should be treated as a response (i.e. $resource.interceptor.responseError). Alternatively, I can argue that since the error occurred during request preprocessing, it's still a request and therefore should be handled by $resource.interceptor.requestError.

My mental model of this is basically a hole. At the bottom of the hole is your response data, and you can only climb down the hole far enough until you get your first error. After that, you have to climb back up through the error interceptors instead of the response interceptors. Thus I'm more in favor of the "Everything from $http is considered a response error" way of thinking of the problem, which from what I understand is how it works now. I'd have to dig into it a bit more to see how the responseError headers and data are constructed to see if that makes sense.

krotscheck avatar Feb 22 '16 13:02 krotscheck

@krotscheck, good points raised. I too lean towards treating anything coming back from the call to $http as "response" (we wouldn't be able to treat is otherwise without hacking $http's behavior too and that's something I'd like to avoid).

In a sense they are not request interceptors (as those in $http's config), they are more like "$http interceptors", i.e. intercepting the call to $http. At this point, I don't think it makes sense to change names (e.g. preHttp/preHttpError/postHttp/postHttpError*), as it would probably be more confusing to developers than helpful.

It's important to point out the bahavior in the documentation, though.

(*: Yeah, I know, I'm not good with names...)

gkalpak avatar Feb 22 '16 19:02 gkalpak

One more thing I discovered: requestInterceptors on $http are arrays of methods. For $resource they're individual methods. Probably something we should update.

krotscheck avatar Feb 23 '16 12:02 krotscheck

I'm not sure if it's useful to change the behavior of the resource interceptors at this point. It would be good for consistency but if we change responseInterceptors to run before the resource specific transformations, devs might have to change their code for little gain (because they relied on the previous behavior).

It might be enpugh to have requestInterceptors that work analogue to responseInterceptors for consistency: https://github.com/angular/angular.js/pull/15674, and document how exactly the interceptors work.

A bigger, non-breaking change would be to introduce local http interceptors that work like global http interceptors, but only for single requests. Those could then be passed to resource as a httpOption. This would also make it possible to change the headers in an individual request again: https://github.com/angular/angular.js/issues/11438

Narretz avatar Nov 01 '17 12:11 Narretz

I am working on a PR that will change the raltive order (and bahavior) response interceptors and success/error callbacks in the following way:


Before: resource interceptors 1 6 x


After: resource interceptors 1 7 x


I think it is reasonable enough (even if not perfect).

  • For people not using success/error callbacks, it should make no difference.
  • For people using success/error callbacks (with or without response interceptors), the main difference will be that throwing inside the callbacks will not propagate to $promise. (I don't think this matters much, since using these callbacks is discouraged anyway.)
  • In addition, for people using success/error callbacks with response interceptors, another difference will be that the callbacks will always be run after the interceptors and wait for them to resolve (in case they return a promise), which gives the interceptor the chance to "course correct" before hitting the callbacks (e.g. recovering from a failure or failing from a successful response ¯\_(ツ)_/¯). For reference, previously the error callback was called before the interceptor and the success callback was called after the interceptor but without waiting for any returned promise to resolve.

gkalpak avatar Feb 10 '18 13:02 gkalpak