fix(core): act operator doesn't catch errors outside stream
MODIFY
packages/core/src/operators/act/act.operator.spec.ts
packages/core/src/operators/act/act.operator.spec.ts
@@ -182,4 +182,34 @@ describe('#act operator', () => {
['-a-(bb)-c-d---', { a: event1, b: eventErrored, c: event3, d: event4 }],
]);
});
+
+ test('catches errored event when thrown outside stream => maps to observable of new events => continues stream', () => {
+ // given
+ const event1: Event = { type: 'TEST_EVENT_1', payload: 1 };
+ const event2: Event = { type: 'TEST_EVENT_2', payload: 2 };
+ const event3: Event = { type: 'TEST_EVENT_3', payload: 3 };
+ const event4: Event = { type: 'TEST_EVENT_4', payload: 4 };
+ const eventErrored: Event = { type: 'TEST_ERROR' };
+
+ // when
+ const effect$ = (event$: Observable<Event>) =>
+ event$.pipe(
+ act(
+ (event: Event) => {
+ if (event.payload === 2) throw new Error('something went wrong');
+ else return of(event);
+ },
+ () => concat(
+ of(eventErrored),
+ of(eventErrored),
+ ),
+ ),
+ );
+
+ // then
+ Marbles.assertEffect(effect$, [
+ ['-a-b----c-d---', { a: event1, b: event2, c: event3, d: event4 }],
+ ['-a-(bb)-c-d---', { a: event1, b: eventErrored, c: event3, d: event4 }],
+ ]);
+ });
});

MODIFY
packages/core/src/operators/act/act.operator.ts
packages/core/src/operators/act/act.operator.ts
@@ -35,17 +35,23 @@ export function act<
metadata: event.metadata,
} as ErrorEvent);
- return (source: Observable<InputEvent>): Observable<CallEvent | ErrorEvent> =>
- source.pipe(
- mergeMap(event => defer(() =>
- callFn(event).pipe(
- catchError(error => pipe(
+ const handleError = (event: InputEvent) => (error: unknown) => pipe(
O.fromNullable(errorFn),
O.map(fn => fn(error, event)),
O.map(res => !isObservable(res) ? of(res) : res),
O.getOrElse(() => getDefaultErrorEvent(error)(event)),
- )),
- ),
- )),
+ );
+
+ return (source: Observable<InputEvent>): Observable<CallEvent | ErrorEvent> =>
+ source.pipe(
+ mergeMap(event => defer(() => {
+ try {
+ return pipe(
+ callFn(event),
+ catchError(handleError(event)));
+ } catch (error) {
+ return handleError(event)(error);
+ }
+ })),
);
}

fix(core): act operator doesn't catch errors outside stream