Skip to content

Commit 32df3f0

Browse files
sylvaindumontbrandonroberts
authored andcommitted
fix(StoreDevtools): report errors to ErrorHandler instead of console
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
1 parent 0e17aad commit 32df3f0

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

modules/store-devtools/src/devtools.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable } from '@angular/core';
1+
import { Injectable, Inject, OnDestroy, ErrorHandler } from '@angular/core';
22
import {
33
Action,
44
ActionReducer,
@@ -39,13 +39,15 @@ export class StoreDevtools implements Observer<any> {
3939
reducers$: ReducerObservable,
4040
extension: DevtoolsExtension,
4141
scannedActions: ScannedActionsSubject,
42+
errorHandler: ErrorHandler,
4243
@Inject(INITIAL_STATE) initialState: any,
4344
@Inject(STORE_DEVTOOLS_CONFIG) config: StoreDevtoolsConfig
4445
) {
4546
const liftedInitialState = liftInitialState(initialState, config.monitor);
4647
const liftReducer = liftReducerWith(
4748
initialState,
4849
liftedInitialState,
50+
errorHandler,
4951
config.monitor,
5052
config
5153
);

modules/store-devtools/src/reducer.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ErrorHandler } from '@angular/core';
12
import {
23
Action,
34
ActionReducer,
@@ -56,7 +57,8 @@ function computeNextEntry(
5657
reducer: ActionReducer<any, any>,
5758
action: Action,
5859
state: any,
59-
error: any
60+
error: any,
61+
errorHandler: ErrorHandler
6062
) {
6163
if (error) {
6264
return {
@@ -71,7 +73,7 @@ function computeNextEntry(
7173
nextState = reducer(state, action);
7274
} catch (err) {
7375
nextError = err.toString();
74-
console.error(err.stack || err);
76+
errorHandler.handleError(err.stack || err);
7577
}
7678

7779
return {
@@ -90,7 +92,8 @@ function recomputeStates(
9092
committedState: any,
9193
actionsById: LiftedActions,
9294
stagedActionIds: number[],
93-
skippedActionIds: number[]
95+
skippedActionIds: number[],
96+
errorHandler: ErrorHandler
9497
) {
9598
// Optimization: exit early and return the same reference
9699
// if we know nothing could have changed.
@@ -113,7 +116,13 @@ function recomputeStates(
113116
const shouldSkip = skippedActionIds.indexOf(actionId) > -1;
114117
const entry: ComputedState = shouldSkip
115118
? previousEntry
116-
: computeNextEntry(reducer, action, previousState, previousError);
119+
: computeNextEntry(
120+
reducer,
121+
action,
122+
previousState,
123+
previousError,
124+
errorHandler
125+
);
117126

118127
nextComputedStates.push(entry);
119128
}
@@ -143,6 +152,7 @@ export function liftInitialState(
143152
export function liftReducerWith(
144153
initialCommittedState: any,
145154
initialLiftedState: LiftedState,
155+
errorHandler: ErrorHandler,
146156
monitorReducer?: any,
147157
options: Partial<StoreDevtoolsConfig> = {}
148158
) {
@@ -337,7 +347,8 @@ export function liftReducerWith(
337347
committedState,
338348
actionsById,
339349
stagedActionIds,
340-
skippedActionIds
350+
skippedActionIds,
351+
errorHandler
341352
);
342353

343354
commitExcessActions(stagedActionIds.length - options.maxAge);
@@ -365,7 +376,8 @@ export function liftReducerWith(
365376
committedState,
366377
actionsById,
367378
stagedActionIds,
368-
skippedActionIds
379+
skippedActionIds,
380+
errorHandler
369381
);
370382

371383
commitExcessActions(stagedActionIds.length - options.maxAge);
@@ -393,7 +405,8 @@ export function liftReducerWith(
393405
committedState,
394406
actionsById,
395407
stagedActionIds,
396-
skippedActionIds
408+
skippedActionIds,
409+
errorHandler
397410
);
398411

399412
// Recompute state history with latest reducer and update action
@@ -429,7 +442,8 @@ export function liftReducerWith(
429442
committedState,
430443
actionsById,
431444
stagedActionIds,
432-
skippedActionIds
445+
skippedActionIds,
446+
errorHandler
433447
);
434448
monitorState = monitorReducer(monitorState, liftedAction);
435449

0 commit comments

Comments
 (0)