Skip to content

Commit

Permalink
fix: normalize promise now have the ability to treat errors properly
Browse files Browse the repository at this point in the history
  • Loading branch information
yagovelazquezfreestar committed Sep 5, 2024
1 parent 6cbdba1 commit 13cdca8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,11 +1041,19 @@ export function attachIdSystem(submodule) {
}
}

function normalizePromise(fn) {
function normalizePromise(fn, handleErrors = false, fnName) {
// for public methods that return promises, make sure we return a "normal" one - to avoid
// exposing confusing stack traces
return function() {
return Promise.resolve(fn.apply(this, arguments));
const promise = Promise.resolve(fn.apply(this, arguments));

if (handleErrors) {
return promise.catch(error => {
logError(`Error occurred in ${fnName}: ${error.message || error}`);
});
}

return promise;
}
}

Expand Down Expand Up @@ -1088,7 +1096,7 @@ export function init(config, {delay = GreedyPromise.timeout} = {}) {
(getGlobal()).getUserIdsAsEids = getUserIdsAsEids;
(getGlobal()).getEncryptedEidsForSource = normalizePromise(getEncryptedEidsForSource);
(getGlobal()).registerSignalSources = registerSignalSources;
(getGlobal()).refreshUserIds = normalizePromise(refreshUserIds);
(getGlobal()).refreshUserIds = normalizePromise(refreshUserIds, true, 'refreshUserIds');
(getGlobal()).getUserIdsAsync = normalizePromise(getUserIdsAsync);
(getGlobal()).getUserIdsAsEidBySource = getUserIdsAsEidBySource;
}
Expand Down

0 comments on commit 13cdca8

Please sign in to comment.