Exclude instrumentation calls from stack trace

This commit is contained in:
Zalmoxisus 2018-12-10 19:03:35 +02:00
parent ee439fac9f
commit d94c034f07
2 changed files with 12 additions and 7 deletions

View File

@ -23,7 +23,7 @@ export const ActionTypes = {
* Action creators to change the History state. * Action creators to change the History state.
*/ */
export const ActionCreators = { export const ActionCreators = {
performAction(action, trace) { performAction(action, trace, toExcludeFromTrace) {
if (!isPlainObject(action)) { if (!isPlainObject(action)) {
throw new Error( throw new Error(
'Actions must be plain objects. ' + 'Actions must be plain objects. ' +
@ -39,9 +39,15 @@ export const ActionCreators = {
} }
let stack; let stack;
let error;
if (trace) { if (trace) {
if (typeof trace === 'function') stack = trace(action); if (typeof trace === 'function') stack = trace(action);
else stack = Error().stack; else {
error = Error();
// https://v8.dev/docs/stack-trace-api#stack-trace-collection-for-custom-exceptions
if (Error.captureStackTrace) Error.captureStackTrace(error, toExcludeFromTrace);
stack = error.stack;
}
} }
return { type: ActionTypes.PERFORM_ACTION, action, timestamp: Date.now(), stack }; return { type: ActionTypes.PERFORM_ACTION, action, timestamp: Date.now(), stack };
@ -191,8 +197,8 @@ function recomputeStates(
/** /**
* Lifts an app's action into an action on the lifted store. * Lifts an app's action into an action on the lifted store.
*/ */
export function liftAction(action, trace) { export function liftAction(action, trace, toExcludeFromTrace) {
return ActionCreators.performAction(action, trace); return ActionCreators.performAction(action, trace, toExcludeFromTrace);
} }
/** /**
@ -614,7 +620,7 @@ export function unliftStore(liftedStore, liftReducer, options) {
liftedStore, liftedStore,
dispatch(action) { dispatch(action) {
liftedStore.dispatch(liftAction(action, trace)); liftedStore.dispatch(liftAction(action, trace, this.dispatch));
return action; return action;
}, },

View File

@ -710,8 +710,7 @@ describe('instrument', () => {
expect(exportedState.actionsById[0].stack).toBe(undefined); expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string'); expect(exportedState.actionsById[1].stack).toBeA('string');
expect(exportedState.actionsById[1].stack).toMatch(/^Error/); expect(exportedState.actionsById[1].stack).toMatch(/^Error/);
expect(exportedState.actionsById[1].stack).toContain('at Object.performAction'); expect(exportedState.actionsById[1].stack).toNotMatch(/instrument.js/);
expect(exportedState.actionsById[1].stack).toContain('instrument.js');
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js'); expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack).toContain('/mocha/'); expect(exportedState.actionsById[1].stack).toContain('/mocha/');
}); });