Skip to content

Commit

Permalink
Wrap the catch method also (#367)
Browse files Browse the repository at this point in the history
* Wrap the catch method also

* Add tests for capture promise

* Fixed failing tests...
  • Loading branch information
DavidTanner committed Jan 28, 2021
1 parent d970e7c commit dc7ec53
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 98 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ To send us a pull request, please:
5. Send us a pull request, answering any default questions in the pull request interface.
6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation.

GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
GitHub provides additional documents on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).


Expand Down
2 changes: 1 addition & 1 deletion packages/core/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
]
},
"parserOptions": {
"ecmaVersion": 6
"ecmaVersion": 2017
}
}
29 changes: 22 additions & 7 deletions packages/core/lib/patchers/promise_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,45 @@
* so all subsegments generated within Promise are attached to the correct parent.
*/

var contextUtils = require('../context_utils');
const contextUtils = require('../context_utils');

function patchPromise(Promise) {
var then = Promise.prototype.then;
const then = Promise.prototype.then;
Promise.prototype.then = function(onFulfilled, onRejected) {
if (contextUtils.isAutomaticMode()
&& tryGetCurrentSegment()
) {
var ns = contextUtils.getNamespace();
const ns = contextUtils.getNamespace();

onFulfilled = onFulfilled && ns.bind(onFulfilled);
onRejected = onRejected && ns.bind(onRejected);
}

return then.call(this, onFulfilled, onRejected);
};

const origCatch = Promise.prototype.catch;
if (origCatch) {
Promise.prototype.catch = function (onRejected) {
if (contextUtils.isAutomaticMode()
&& tryGetCurrentSegment()
) {
const ns = contextUtils.getNamespace();

onRejected = onRejected && ns.bind(onRejected);
}

return origCatch.call(this, onRejected);
};
}
}

function tryGetCurrentSegment() {
var segment = null;
try {
segment = contextUtils.getSegment();
} catch(e) { /**/ }
return segment;
return contextUtils.getSegment();
} catch(e) {
return undefined;
}
}

function capturePromise() {
Expand Down
1 change: 1 addition & 0 deletions packages/core/test/unit/context_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('ContextUtils', function() {

beforeEach(function() {
sandbox = sinon.createSandbox();
sinon.restore();
});

afterEach(function() {
Expand Down

0 comments on commit dc7ec53

Please sign in to comment.