Skip to content

Commit

Permalink
feat(StoreDevtools): catch redux devtools errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Nov 29, 2018
1 parent 9d8e55b commit 4c83716
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
39 changes: 39 additions & 0 deletions modules/store-devtools/spec/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,43 @@ describe('DevtoolsExtension', () => {
);
});
});

describe('error handling', () => {
let consoleSpy: jasmine.Spy;

beforeEach(() => {
devtoolsExtension = new DevtoolsExtension(
reduxDevtoolsExtension,
createConfig({}),
<any>null
);
// Subscription needed or else extension connection will not be established.
devtoolsExtension.actions$.subscribe();
consoleSpy = spyOn(console, 'warn');
});

it('for normal action', () => {
(extensionConnection.send as jasmine.Spy).and.callFake(() => {
throw new Error('uh-oh something went wrong');
});

const action = new PerformAction({ type: 'FOO' }, 1234567);
const state = createState();

devtoolsExtension.notify(action, state);
expect(consoleSpy).toHaveBeenCalled();
});

it('for action that requires full state update', () => {
(reduxDevtoolsExtension.send as jasmine.Spy).and.callFake(() => {
throw new Error('uh-oh something went wrong');
});

const action = {} as LiftedAction;
const state = createState();

devtoolsExtension.notify(action, state);
expect(consoleSpy).toHaveBeenCalled();
});
});
});
27 changes: 22 additions & 5 deletions modules/store-devtools/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export class DevtoolsExtension {
state.nextActionId
)
: action;
this.extensionConnection.send(sanitizedAction, sanitizedState);

this.sendToReduxDevtools(() =>
this.extensionConnection.send(sanitizedAction, sanitizedState)
);
} else {
// Requires full state update
const sanitizedLiftedState = {
Expand All @@ -146,10 +149,13 @@ export class DevtoolsExtension {
? sanitizeStates(this.config.stateSanitizer, state.computedStates)
: state.computedStates,
};
this.devtoolsExtension.send(
null,
sanitizedLiftedState,
this.getExtensionConfig(this.config)

this.sendToReduxDevtools(() =>
this.devtoolsExtension.send(
null,
sanitizedLiftedState,
this.getExtensionConfig(this.config)
)
);
}
}
Expand Down Expand Up @@ -250,4 +256,15 @@ export class DevtoolsExtension {
}
return extensionOptions;
}

private sendToReduxDevtools(send: Function) {
try {
send();
} catch (err) {
console.warn(
'@ngrx/store-devtools: something went wrong inside the redux devtools',
err
);
}
}
}

0 comments on commit 4c83716

Please sign in to comment.