-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$resource can decorate arrays and errors #16079
Conversation
Thx for the PR. This has been discussed before (e.g. #6138) and is generally somewhat problematic (i.e. there doesn't seem to be a clean/non-magical way to do it). Note that as the complexity gets higher, it is often simpler and more maintainable to use the low-level More specifically about this PR:
|
The 'errorDecorate' options allows copying object properties from the response to the resource, even where the request was rejected. You can see the response body in the catch() handler response, but you cannot copy properties from the response to the resource, from a transformResponse. This change makes it so those properties are copied over to the resource object.
The arrayDecorate option allows copying non-array indice object properties from the response to the result resource array, for resource collection. This is helpful when the response contains an object containing the collection as well as meta-data related to the collection. For example:
Which through transformResponse gets converted to an array with a 'meta' property. This change makes it so that these properties are copied to the resource, where normally only array indices are pushed and properties on the array ignored. |
I may be able to avoid this change, by copying |
I don't think this a good practice that we should encourage. If a requests fails, it is very confusing to still have a useful resource instance (or array). Again, if you need something special for your usecase, it is preferrable to create your own wrapper around
Yeah, but when returning JSON data it is not possible to have both array indices and metadata properties.
In that case there will be no items in the array and the collection items will not be Resource instances (which beats the purpose of using After that, I should then be able to access the original resource object ( As mentioned above, I don't like the idea of attaching error metadata to a resource instance (or array) 😁 |
What makes it useful? You always get back a resource instance from a request. How is it confusing, when you always have a resource object returned from the request, in the current implementation? I can't just add a wrapper around $http or $resource, because the resource created is a private variable until it is added to response.resource in the then clause.
Hence why my example doesn't show that:
Which requires transformation, obviously.
Nonsense. Do you not see the part of the code that copies over the array indices and then separately the non-numeric object properties? They are added as Resources, nothing changes there. The meta properties wouldn't be Resources, but then why would you expect them to be? You don't iterate the object properties when you iterate the array!
My suggestion at the end was not to attach any meta data, but only to give the interceptors.responseError access to the response.resource, which would make this change moot and give me everything I need to implement this locally. Basically just add this to the $http request:
The only problem I have with the above is that the explicit rejection fails a bunch of resourceSpec tests that expected a failure, but not the explicit rejection, and errors "possibly unhandled rejection". |
We obviously see things different, so there is no point in keep arguing 😃 I tried adding it and no test failed (as expected, since there is already a rejection): promise = $http(config).then(function(response) {
// Handle success
// ...
}, function(response) {
// Handle failure
response.resource = value;
return $q.reject(response);
}); |
Closing this in favor of #16109 |
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Adds ability to use 'arrayDecorate' and 'errorDecorate' options to decorate a resource with additional properties in the response. The arrayDecorate option allows copying non-array indice object properties from the response to the result resource array, for resource collection. This is helpful when the response contains an object containing the collection as well as meta-data related to the collection. The 'errorDecorate' options allows copying object properties from the response to the resource, even where the request was rejected. This is helpful when the request fails, but the API returns useful error messages.
Does this PR introduce a breaking change?
No
Please check if the PR fulfills these requirements
Other information: