Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap the catch method also #367

Merged
merged 3 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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