-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(ngResource): do not eat exceptions in success callback #15628
Conversation
move the promise catch noop call inside of error callback handler so that errors inside of the success callback are not eaten added two tests to prove functionality No breaking changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks good. I've left a comment regarding the tests.
test/ngResource/resourceSpec.js
Outdated
@@ -1978,4 +1978,39 @@ describe('configuring `cancellable` on the provider', function() { | |||
expect(creditCard3.$cancelRequest).toBeDefined(); | |||
}); | |||
}); | |||
|
|||
describe('exception handling in callbacks', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests should be incorporated into the errors block. Also, can you add tests for all 4 combinations (no errorCallback/no responseErrorInterceptor, no errorCallback/yes responseErrorInterceptor, yes errorCallback/no responseErrorInterceptor, yes errorCallback/yes responseErrorInterceptor).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks I made the changes requested!
…ded two more cases to cover all the scenarios. No breaking changes
Thanks I made the changes |
test/ngResource/resourceSpec.js
Outdated
|
||
it('should throw exception in success callback when error callback provided and no responseErrorInterceptor', function() { | ||
$httpBackend.expect('GET', '/CreditCard/123').respond({ id: 123, number: '9876' }); | ||
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the unnecessary bits (here and below). E.g.:
$httpBackend.expect('GET', '/CreditCard/123').respond(null);
var CreditCard = $resource('/CreditCard/:id');
test/ngResource/resourceSpec.js
Outdated
@@ -1709,6 +1709,79 @@ describe('handling rejections', function() { | |||
expect($exceptionHandler.errors[0]).toMatch(/^Possibly unhandled rejection/); | |||
}) | |||
); | |||
|
|||
|
|||
it('should throw exception in success callback when error callback provided and no responseErrorInterceptor', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the tests descriptions to something like:
should not swallow exception in success callback...
test/ngResource/resourceSpec.js
Outdated
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' }, { | ||
'get': { | ||
method: 'GET', | ||
interceptor: { responseError: function() { return $q.reject({}); } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this a non-rejecting interceptor.
test/ngResource/resourceSpec.js
Outdated
var CreditCard = $resource('/CreditCard/:id:verb', { id: '@id.key' }, { | ||
'get': { | ||
method: 'GET', | ||
interceptor: { responseError: function() { return $q.reject({}); } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make this a non-rejecting interceptor.
…interceptors to non rejecting ones
Thanks I fixed up the tests etc |
LGTM 👍 |
Previously, errors thrown inside the `success` callback would be swallowed by a noop `catch()` handler. The `catch()` handler was added in order to avoid an unnecessary "Possibly Unhandled Rejection" error, in case the user provided an `error` callback (which would handle request errors). The handler was added too "eagrly" and as a result would swallow errors thrown in the `success` callback, despite the fact that those errors would _not_ be handled by the `error` callback. This commit fixes this, by adding the `catch()` handler "lazily", only when it is certain that a rejection will be handled by the `error` callback. Fixes #15624 Closes #15628
Previously, errors thrown inside the `success` callback would be swallowed by a noop `catch()` handler. The `catch()` handler was added in order to avoid an unnecessary "Possibly Unhandled Rejection" error, in case the user provided an `error` callback (which would handle request errors). The handler was added too "eagrly" and as a result would swallow errors thrown in the `success` callback, despite the fact that those errors would _not_ be handled by the `error` callback. This commit fixes this, by adding the `catch()` handler "lazily", only when it is certain that a rejection will be handled by the `error` callback. Fixes angular#15624 Closes angular#15628
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix
What is the current behavior? (You can also link to an open issue here)
Addresses bug identified in #15624.
No error message is output in the console:
Error message is output in the console:
What is the new behavior (if this is a feature change)?
Now any exceptions thrown in the success callback will be thrown and not eaten.
Does this PR introduce a breaking change?
No
Please check if the PR fulfills these requirements
Other information: