Skip to content

Commit

Permalink
fix(StoreDevtools): report errors to ErrorHandler instead of console
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Errors in reducers are no longer hidden from ErrorHandler by
StoreDevtools

BEFORE:

Errors in reducers are caught by StoreDevtools and logged to the console

AFTER:

Errors in reducers are reported to ErrorHandler
  • Loading branch information
sylvaindumont authored and brandonroberts committed Mar 30, 2018
1 parent 0e17aad commit 32df3f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
4 changes: 3 additions & 1 deletion modules/store-devtools/src/devtools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from '@angular/core';
import { Injectable, Inject, OnDestroy, ErrorHandler } from '@angular/core';
import {
Action,
ActionReducer,
Expand Down Expand Up @@ -39,13 +39,15 @@ export class StoreDevtools implements Observer<any> {
reducers$: ReducerObservable,
extension: DevtoolsExtension,
scannedActions: ScannedActionsSubject,
errorHandler: ErrorHandler,
@Inject(INITIAL_STATE) initialState: any,
@Inject(STORE_DEVTOOLS_CONFIG) config: StoreDevtoolsConfig
) {
const liftedInitialState = liftInitialState(initialState, config.monitor);
const liftReducer = liftReducerWith(
initialState,
liftedInitialState,
errorHandler,
config.monitor,
config
);
Expand Down
30 changes: 22 additions & 8 deletions modules/store-devtools/src/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ErrorHandler } from '@angular/core';
import {
Action,
ActionReducer,
Expand Down Expand Up @@ -56,7 +57,8 @@ function computeNextEntry(
reducer: ActionReducer<any, any>,
action: Action,
state: any,
error: any
error: any,
errorHandler: ErrorHandler
) {
if (error) {
return {
Expand All @@ -71,7 +73,7 @@ function computeNextEntry(
nextState = reducer(state, action);
} catch (err) {
nextError = err.toString();
console.error(err.stack || err);
errorHandler.handleError(err.stack || err);
}

return {
Expand All @@ -90,7 +92,8 @@ function recomputeStates(
committedState: any,
actionsById: LiftedActions,
stagedActionIds: number[],
skippedActionIds: number[]
skippedActionIds: number[],
errorHandler: ErrorHandler
) {
// Optimization: exit early and return the same reference
// if we know nothing could have changed.
Expand All @@ -113,7 +116,13 @@ function recomputeStates(
const shouldSkip = skippedActionIds.indexOf(actionId) > -1;
const entry: ComputedState = shouldSkip
? previousEntry
: computeNextEntry(reducer, action, previousState, previousError);
: computeNextEntry(
reducer,
action,
previousState,
previousError,
errorHandler
);

nextComputedStates.push(entry);
}
Expand Down Expand Up @@ -143,6 +152,7 @@ export function liftInitialState(
export function liftReducerWith(
initialCommittedState: any,
initialLiftedState: LiftedState,
errorHandler: ErrorHandler,
monitorReducer?: any,
options: Partial<StoreDevtoolsConfig> = {}
) {
Expand Down Expand Up @@ -337,7 +347,8 @@ export function liftReducerWith(
committedState,
actionsById,
stagedActionIds,
skippedActionIds
skippedActionIds,
errorHandler
);

commitExcessActions(stagedActionIds.length - options.maxAge);
Expand Down Expand Up @@ -365,7 +376,8 @@ export function liftReducerWith(
committedState,
actionsById,
stagedActionIds,
skippedActionIds
skippedActionIds,
errorHandler
);

commitExcessActions(stagedActionIds.length - options.maxAge);
Expand Down Expand Up @@ -393,7 +405,8 @@ export function liftReducerWith(
committedState,
actionsById,
stagedActionIds,
skippedActionIds
skippedActionIds,
errorHandler
);

// Recompute state history with latest reducer and update action
Expand Down Expand Up @@ -429,7 +442,8 @@ export function liftReducerWith(
committedState,
actionsById,
stagedActionIds,
skippedActionIds
skippedActionIds,
errorHandler
);
monitorState = monitorReducer(monitorState, liftedAction);

Expand Down

0 comments on commit 32df3f0

Please sign in to comment.