Skip to content

Commit 8f297d1

Browse files
jbedardbrandonroberts
authored andcommitted
feat(ErrorHandler): Use the Angular ErrorHandler for reporting errors (#667)
Closes #626 BREAKING CHANGE: The ErrorReporter has been replaced with ErrorHandler from angular/core. BEFORE: Errors were reported to the ngrx/effects ErrorReporter. The ErrorReporter would log to the console by default. AFTER: Errors are now reported to the @angular/core ErrorHandler.
1 parent 77c832a commit 8f297d1

File tree

7 files changed

+21
-100
lines changed

7 files changed

+21
-100
lines changed

modules/effects/spec/effect_sources.spec.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,19 @@ import { _throw } from 'rxjs/observable/throw';
99
import { never } from 'rxjs/observable/never';
1010
import { empty } from 'rxjs/observable/empty';
1111
import { TestBed } from '@angular/core/testing';
12-
import { ErrorReporter } from '../src/error_reporter';
13-
import { CONSOLE } from '../src/tokens';
12+
import { ErrorHandler } from '@angular/core';
1413
import { Effect, EffectSources } from '../';
1514

1615
describe('EffectSources', () => {
17-
let mockErrorReporter: ErrorReporter;
16+
let mockErrorReporter: ErrorHandler;
1817
let effectSources: EffectSources;
1918

2019
beforeEach(() => {
2120
TestBed.configureTestingModule({
22-
providers: [
23-
EffectSources,
24-
ErrorReporter,
25-
{
26-
provide: CONSOLE,
27-
useValue: console,
28-
},
29-
],
21+
providers: [EffectSources],
3022
});
3123

32-
mockErrorReporter = TestBed.get(ErrorReporter);
24+
mockErrorReporter = TestBed.get(ErrorHandler);
3325
effectSources = TestBed.get(EffectSources);
3426

3527
spyOn(mockErrorReporter, 'handleError');
@@ -118,7 +110,7 @@ describe('EffectSources', () => {
118110
});
119111

120112
function toActions(source: any): Observable<any> {
121-
source['errorReporter'] = mockErrorReporter;
113+
source['errorHandler'] = mockErrorReporter;
122114
return effectSources.toActions.call(source);
123115
}
124116
});

modules/effects/src/effect_notification.ts

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { Observable } from 'rxjs/Observable';
22
import { Notification } from 'rxjs/Notification';
33
import { Action } from '@ngrx/store';
4-
import {
5-
ErrorReporter,
6-
EffectError,
7-
InvalidActionError,
8-
} from './error_reporter';
4+
import { ErrorHandler } from '@angular/core';
95

106
export interface EffectNotification {
117
effect: Observable<any> | (() => Observable<any>);
@@ -17,49 +13,34 @@ export interface EffectNotification {
1713

1814
export function verifyOutput(
1915
output: EffectNotification,
20-
reporter: ErrorReporter
16+
reporter: ErrorHandler
2117
) {
2218
reportErrorThrown(output, reporter);
2319
reportInvalidActions(output, reporter);
2420
}
2521

26-
function reportErrorThrown(
27-
output: EffectNotification,
28-
reporter: ErrorReporter
29-
) {
22+
function reportErrorThrown(output: EffectNotification, reporter: ErrorHandler) {
3023
if (output.notification.kind === 'E') {
31-
const errorReason = new Error(
32-
`Effect ${getEffectName(output)} threw an error`
33-
) as EffectError;
34-
35-
errorReason.Source = output.sourceInstance;
36-
errorReason.Effect = output.effect;
37-
errorReason.Error = output.notification.error;
38-
errorReason.Notification = output.notification;
39-
40-
reporter.handleError(errorReason);
24+
reporter.handleError(output.notification.error);
4125
}
4226
}
4327

4428
function reportInvalidActions(
4529
output: EffectNotification,
46-
reporter: ErrorReporter
30+
reporter: ErrorHandler
4731
) {
4832
if (output.notification.kind === 'N') {
4933
const action = output.notification.value;
5034
const isInvalidAction = !isAction(action);
5135

5236
if (isInvalidAction) {
53-
const errorReason = new Error(
54-
`Effect ${getEffectName(output)} dispatched an invalid action`
55-
) as InvalidActionError;
56-
57-
errorReason.Source = output.sourceInstance;
58-
errorReason.Effect = output.effect;
59-
errorReason.Dispatched = action;
60-
errorReason.Notification = output.notification;
61-
62-
reporter.handleError(errorReason);
37+
reporter.handleError(
38+
new Error(
39+
`Effect ${getEffectName(output)} dispatched an invalid action: ${
40+
action
41+
}`
42+
)
43+
);
6344
}
6445
}
6546
}

modules/effects/src/effect_sources.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ import { concat } from 'rxjs/observable/concat';
88
import { Observable } from 'rxjs/Observable';
99
import { Subject } from 'rxjs/Subject';
1010
import { Notification } from 'rxjs/Notification';
11-
import { Injectable } from '@angular/core';
11+
import { ErrorHandler, Injectable } from '@angular/core';
1212
import { Action } from '@ngrx/store';
1313
import { EffectNotification, verifyOutput } from './effect_notification';
1414
import { getSourceForInstance } from './effects_metadata';
1515
import { resolveEffectSource } from './effects_resolver';
16-
import { ErrorReporter } from './error_reporter';
1716

1817
@Injectable()
1918
export class EffectSources extends Subject<any> {
20-
constructor(private errorReporter: ErrorReporter) {
19+
constructor(private errorHandler: ErrorHandler) {
2120
super();
2221
}
2322

@@ -37,7 +36,7 @@ export class EffectSources extends Subject<any> {
3736
map.call(
3837
exhaustMap.call(source$, resolveEffectSource),
3938
(output: EffectNotification) => {
40-
verifyOutput(output, this.errorReporter);
39+
verifyOutput(output, this.errorHandler);
4140

4241
return output.notification;
4342
}

modules/effects/src/effects_module.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { NgModule, ModuleWithProviders, Type } from '@angular/core';
22
import { EffectSources } from './effect_sources';
33
import { Actions } from './actions';
4-
import { ROOT_EFFECTS, FEATURE_EFFECTS, CONSOLE } from './tokens';
4+
import { ROOT_EFFECTS, FEATURE_EFFECTS } from './tokens';
55
import { EffectsFeatureModule } from './effects_feature_module';
66
import { EffectsRootModule } from './effects_root_module';
77
import { EffectsRunner } from './effects_runner';
8-
import { ErrorReporter } from './error_reporter';
98

109
@NgModule({})
1110
export class EffectsModule {
@@ -30,18 +29,13 @@ export class EffectsModule {
3029
providers: [
3130
EffectsRunner,
3231
EffectSources,
33-
ErrorReporter,
3432
Actions,
3533
rootEffects,
3634
{
3735
provide: ROOT_EFFECTS,
3836
deps: rootEffects,
3937
useFactory: createSourceInstances,
4038
},
41-
{
42-
provide: CONSOLE,
43-
useFactory: getConsole,
44-
},
4539
],
4640
};
4741
}
@@ -50,7 +44,3 @@ export class EffectsModule {
5044
export function createSourceInstances(...instances: any[]) {
5145
return instances;
5246
}
53-
54-
export function getConsole() {
55-
return console;
56-
}

modules/effects/src/error_reporter.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

modules/effects/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ export { EffectSources } from './effect_sources';
1010
export { OnRunEffects } from './on_run_effects';
1111
export { toPayload } from './util';
1212
export { EffectNotification } from './effect_notification';
13-
export { ErrorReporter } from './error_reporter';
1413
export { ROOT_EFFECTS_INIT } from './effects_root_module';

modules/effects/src/tokens.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ export const ROOT_EFFECTS = new InjectionToken<Type<any>[]>(
99
export const FEATURE_EFFECTS = new InjectionToken<any[][]>(
1010
'ngrx/effects: Feature Effects'
1111
);
12-
export const CONSOLE = new InjectionToken<Console>('Browser Console');

0 commit comments

Comments
 (0)