Skip to content

Commit 23c846b

Browse files
authored
fix(component): capture errors from observable when using ngrxPush pipe and ngrxLet directive
Closes #3100
1 parent b67af2f commit 23c846b

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ logs
33
*.log
44
.nyc
55
.nyc_output
6+
.history
67

78
# Runtime data
89
pids

modules/component/spec/core/cd-aware/cd-aware_creator.spec.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OnDestroy } from '@angular/core';
1+
import { ErrorHandler, OnDestroy } from '@angular/core';
22
import {
33
concat,
44
EMPTY,
@@ -9,7 +9,6 @@ import {
99
throwError,
1010
Unsubscribable,
1111
} from 'rxjs';
12-
1312
import {
1413
CdAware,
1514
createCdAware,
@@ -35,14 +34,15 @@ class CdAwareImplementation<U> implements OnDestroy {
3534
complete: () => (this.completed = true),
3635
};
3736

38-
constructor() {
37+
constructor(errorHandler: ErrorHandler) {
3938
this.cdAware = createCdAware<U>({
4039
render: createRender({
4140
ngZone: manualInstanceNgZone,
4241
cdRef: new MockChangeDetectorRef(),
4342
}),
4443
updateViewContextObserver: this.updateViewContextObserver,
4544
resetContextObserver: this.resetContextObserver,
45+
errorHandler,
4646
});
4747
this.subscription = this.cdAware.subscribe();
4848
}
@@ -53,16 +53,19 @@ class CdAwareImplementation<U> implements OnDestroy {
5353
}
5454

5555
let cdAwareImplementation: CdAwareImplementation<any>;
56-
const setupCdAwareImplementation = () => {
57-
cdAwareImplementation = new CdAwareImplementation();
56+
const setupCdAwareImplementation = (errorHandler: ErrorHandler) => {
57+
cdAwareImplementation = new CdAwareImplementation(errorHandler);
5858
cdAwareImplementation.renderedValue = undefined;
5959
cdAwareImplementation.error = undefined;
6060
cdAwareImplementation.completed = false;
6161
};
6262

6363
describe('CdAware', () => {
64+
let errorHandlerCb: jest.Mock;
65+
6466
beforeEach(() => {
65-
setupCdAwareImplementation();
67+
errorHandlerCb = jest.fn();
68+
setupCdAwareImplementation({ handleError: errorHandlerCb });
6669
});
6770

6871
it('should be implementable', () => {
@@ -145,12 +148,12 @@ describe('CdAware', () => {
145148
});
146149

147150
it('error handling', () => {
148-
cdAwareImplementation.cdAware.nextPotentialObservable(
149-
throwError('Error!')
150-
);
151+
const error = 'Error!';
152+
cdAwareImplementation.cdAware.nextPotentialObservable(throwError(error));
151153
expect(cdAwareImplementation.renderedValue).toBe(undefined);
152-
expect(cdAwareImplementation.error).toBe('Error!');
154+
expect(cdAwareImplementation.error).toBe(error);
153155
expect(cdAwareImplementation.completed).toBe(false);
156+
expect(errorHandlerCb).toHaveBeenCalledWith(error);
154157
});
155158

156159
it('completion handling', () => {

modules/component/src/core/cd-aware/cd-aware_creator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ErrorHandler } from '@angular/core';
12
import {
23
EMPTY,
34
from,
@@ -35,6 +36,7 @@ export function createCdAware<U>(cfg: {
3536
render: () => void;
3637
resetContextObserver: NextObserver<void>;
3738
updateViewContextObserver: NextObserver<U | undefined | null>;
39+
errorHandler: ErrorHandler;
3840
}): CdAware<U | undefined | null> {
3941
const potentialObservablesSubject: Subject<
4042
ObservableInput<U> | undefined | null
@@ -72,6 +74,7 @@ export function createCdAware<U>(cfg: {
7274
tap(cfg.updateViewContextObserver),
7375
tap(() => cfg.render()),
7476
catchError((e) => {
77+
cfg.errorHandler.handleError(e);
7578
return EMPTY;
7679
})
7780
);

modules/component/src/let/let.directive.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import {
22
ChangeDetectorRef,
33
Directive,
4+
ErrorHandler,
45
Input,
56
NgZone,
67
OnDestroy,
78
TemplateRef,
89
ViewContainerRef,
910
} from '@angular/core';
10-
1111
import { NextObserver, ObservableInput, Observer, Unsubscribable } from 'rxjs';
12-
1312
import { CdAware, createCdAware } from '../core/cd-aware/cd-aware_creator';
1413
import { createRender } from '../core/cd-aware/creator_render';
1514

@@ -157,12 +156,14 @@ export class LetDirective<U> implements OnDestroy {
157156
cdRef: ChangeDetectorRef,
158157
ngZone: NgZone,
159158
private readonly templateRef: TemplateRef<LetViewContext<U>>,
160-
private readonly viewContainerRef: ViewContainerRef
159+
private readonly viewContainerRef: ViewContainerRef,
160+
errorHandler: ErrorHandler
161161
) {
162162
this.cdAware = createCdAware<U>({
163163
render: createRender({ cdRef, ngZone }),
164164
resetContextObserver: this.resetContextObserver,
165165
updateViewContextObserver: this.updateViewContextObserver,
166+
errorHandler,
166167
});
167168
this.subscription = this.cdAware.subscribe();
168169
}

modules/component/src/push/push.pipe.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
22
ChangeDetectorRef,
3+
ErrorHandler,
34
NgZone,
45
OnDestroy,
56
Pipe,
67
PipeTransform,
78
} from '@angular/core';
89
import { NextObserver, ObservableInput, Unsubscribable } from 'rxjs';
9-
1010
import { CdAware, createCdAware } from '../core/cd-aware/cd-aware_creator';
1111
import { createRender } from '../core/cd-aware/creator_render';
1212

@@ -68,11 +68,16 @@ export class PushPipe<S> implements PipeTransform, OnDestroy {
6868
next: (value: S | null | undefined) => (this.renderedValue = value),
6969
};
7070

71-
constructor(cdRef: ChangeDetectorRef, ngZone: NgZone) {
71+
constructor(
72+
cdRef: ChangeDetectorRef,
73+
ngZone: NgZone,
74+
errorHandler: ErrorHandler
75+
) {
7276
this.cdAware = createCdAware<S>({
7377
render: createRender({ cdRef, ngZone }),
7478
updateViewContextObserver: this.updateViewContextObserver,
7579
resetContextObserver: this.resetContextObserver,
80+
errorHandler,
7681
});
7782
this.subscription = this.cdAware.subscribe();
7883
}

0 commit comments

Comments
 (0)