8000 fix(state): handle error emission in `connect` using `ErrorHandler` by edbzn · Pull Request #1553 · rx-angular/rx-angular · GitHub
[go: up one dir, main page]

Skip to content

fix(state): handle error emission in connect using ErrorHandler #1553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(state): handle error emission in connect using ErrorHandler
  • Loading branch information
edbzn committed May 25, 2023
commit fa72532aa36c314185170c213ea3d106d17a206b
4 changes: 2 additions & 2 deletions libs/state/selections/src/lib/accumulation-observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createAccumulationObservable<T extends object>({
stateObservables = new Subject<Observable<Partial<T>>>(),
stateSlices = new Subject<Partial<T>>(),
accumulatorObservable = new BehaviorSubject(defaultAccumulator),
handleError = (e: any) => console.error(e),
handleError = (e: unknown) => console.error(e),
} = {}): Accumulator<T> {
const signal$ = merge(
stateObservables.pipe(
Expand All @@ -49,7 +49,7 @@ export function createAccumulationObservable<T extends object>({
(error) => handleError(error)
),
// @Notice We catch the error here as it get lost in between `publish` and `publishReplay`. We return empty to
catchError((e) => EMPTY),
catchError(() => EMPTY),
publish()
);
const state$: Observable<T> = signal$.pipe(publishReplay(1));
Expand Down
87 changes: 68 additions & 19 deletions libs/state/spec/rx-state.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
ErrorHandler,
Input,
Output,
Type,
ViewChild,
} from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { RxState } from '@rx-angular/state';
import { select } from '@rx-angular/state/selections';
import { PrimitiveState } from '@test-helpers';
Expand Down Expand Up @@ -105,21 +106,17 @@ export class RxStateGlueContainerComponent extends RxState<PrimitiveState> {

describe('LocalProviderTestComponent', () => {
let component: RxStateInjectionComponent;
let fixture: ComponentFixture<RxStateInjectionComponent>;
let errorHandlerSpy: any;
let errorHandlerSpy: jest.Mock;

beforeEach(() => {
const testBed = TestBed.configureTestingModule({
declarations: [RxStateInjectionComponent],
const { component: c, errorHandler: e } = setupTestComponent({
testComponent: RxStateInjectionComponent,
providers: [
{ provide: ErrorHandler, useValue: { handleError: jest.fn() } },
],
teardown: { destroyAfterEach: true },
});
fixture = TestBed.createComponent(RxStateInjectionComponent);
component = fixture.componentInstance;
errorHandlerSpy = testBed.inject(ErrorHandler).handleError;
fixture.detectChanges();
component = c;
errorHandlerSpy = e.handleError as jest.Mock;
});

it('should create', () => {
Expand All @@ -137,21 +134,17 @@ describe('LocalProviderTestComponent', () => {

describe('InheritanceTestComponent', () => {
let component: RxStateInheritanceComponent;
let fixture: ComponentFixture<RxStateInheritanceComponent>;
let errorHandlerSpy: any;
let errorHandlerSpy: jest.Mock;

beforeEach(() => {
const testBed = TestBed.configureTestingModule({
declarations: [RxStateInheritanceComponent],
teardown: { destroyAfterEach: true },
const { component: c, errorHandler: e } = setupTestComponent({
testComponent: RxStateInheritanceComponent,
providers: [
{ provide: ErrorHandler, useValue: { handleError: jest.fn() } },
],
});
fixture = TestBed.createComponent(RxStateInheritanceComponent);
component = fixture.componentInstance;
errorHandlerSpy = testBed.inject(ErrorHandler).handleError;
fixture.detectChanges();
component = c;
errorHandlerSpy = e.handleError as jest.Mock;
});

it('should create', () => {
Expand All @@ -168,3 +161,59 @@ describe('InheritanceTestComponent', () => {
});
});
});

describe('CustomErrorHandler', () => {
let component: RxStateInheritanceComponent;
let errorHandlerSpy: jest.SpyInstance;

class CustomErrorHandler implements ErrorHandler {
private prod = true;
handleError() {
if (this.prod) {
throw new Error('Prod error');
}
throw new Error('Dev error');
}
}

beforeEach(() => {
const { component: c, errorHandler: e } = setupTestComponent({
testComponent: RxStateInheritanceComponent,
providers: [
{
provide: ErrorHandler,
useClass: CustomErrorHandler,
},
],
});
component = c;
errorHandlerSpy = jest.spyOn(e, 'handleError');
});

describe('state.connect', () => {
it('should handle error through CustomErrorHandler', () => {
const error$ = throwError(() => new Error('whoops'));
component.connect('str', error$);
expect(errorHandlerSpy).toThrow(new Error('Prod error'));
});
});
});

function setupTestComponent({
testComponent,
providers,
}: {
testComponent: Type<any>;
providers: any[];
}) {
const testBed = TestBed.configureTestingModule({
declarations: [testComponent],
providers: [...providers],
teardown: { destroyAfterEach: true },
});
const fixture = TestBed.createComponent(testComponent);
const component = fixture.componentInstance;
const errorHandler = testBed.inject(ErrorHandler);

return { fixture, component, errorHandler };
}
4 changes: 2 additions & 2 deletions libs/state/src/lib/rx-state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export type ProjectValueReducer<T, K extends keyof T, V> = (
@Injectable()
export class RxState<T extends object> implements OnDestroy, Subscribable<T> {
private subscription = new Subscription();
private handleError = inject(ErrorHandler).handleError;
private errorHandler = inje 4F97 ct(ErrorHandler);
private accumulator = createAccumulationObservable<T>({
handleError: this.handleError,
handleError: this.errorHandler.handleError.bind(this.errorHandler),
});
private effectObservable = createSideEffectObservable();

Expand Down
0