Skip to content

Commit 4014e19

Browse files
authored
fix: catch within innerSubscribe (#6186)
* test: add failing invalid ObservableInput tests * fix: catch within innerSubscribe Closes #5344
1 parent 02219cc commit 4014e19

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

spec/operators/concatMap-spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,17 @@ describe('Observable.prototype.concatMap', () => {
691691
done(new Error('Subscriber complete handler not supposed to be called.'));
692692
});
693693
});
694+
695+
it('should report invalid observable inputs via error notifications', () => {
696+
const e1 = hot('--1|');
697+
const e1subs = '^ !';
698+
const expected = '--#';
699+
700+
const result = e1.pipe(concatMap(() => null as any));
701+
702+
expectObservable(result).toBe(expected, null, new TypeError(
703+
"You provided 'null' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable."
704+
));
705+
expectSubscriptions(e1.subscriptions).toBe(e1subs);
706+
});
694707
});

spec/operators/exhaustMap-spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,17 @@ describe('exhaustMap', () => {
434434
expectSubscriptions(x.subscriptions).toBe(xsubs);
435435
expectSubscriptions(e1.subscriptions).toBe(e1subs);
436436
});
437+
438+
it('should report invalid observable inputs via error notifications', () => {
439+
const e1 = hot('--1|');
440+
const e1subs = '^ !';
441+
const expected = '--#';
442+
443+
const result = e1.pipe(exhaustMap(() => null as any));
444+
445+
expectObservable(result).toBe(expected, null, new TypeError(
446+
"You provided 'null' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable."
447+
));
448+
expectSubscriptions(e1.subscriptions).toBe(e1subs);
449+
});
437450
});

spec/operators/mergeMap-spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,17 @@ describe('mergeMap', () => {
852852
expectObservable(result).toBe(expected, undefined, noXError);
853853
});
854854
});
855+
856+
it('should report invalid observable inputs via error notifications', () => {
857+
const e1 = hot('--1|');
858+
const e1subs = '^ !';
859+
const expected = '--#';
860+
861+
const result = e1.pipe(mergeMap(() => null as any));
862+
863+
expectObservable(result).toBe(expected, null, new TypeError(
864+
"You provided 'null' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable."
865+
));
866+
expectSubscriptions(e1.subscriptions).toBe(e1subs);
867+
});
855868
});

spec/operators/switchMap-spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,17 @@ describe('switchMap', () => {
444444
expectSubscriptions(x.subscriptions).toBe(xsubs);
445445
expectSubscriptions(e1.subscriptions).toBe(e1subs);
446446
});
447+
448+
it('should report invalid observable inputs via error notifications', () => {
449+
const e1 = hot('--1|');
450+
const e1subs = '^ !';
451+
const expected = '--#';
452+
453+
const result = e1.pipe(switchMap(() => null as any));
454+
455+
expectObservable(result).toBe(expected, null, new TypeError(
456+
"You provided 'null' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable."
457+
));
458+
expectSubscriptions(e1.subscriptions).toBe(e1subs);
459+
});
447460
});

src/internal/innerSubscribe.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,11 @@ export function innerSubscribe(result: any, innerSubscriber: Subscriber<any>): S
110110
if (result instanceof Observable) {
111111
return result.subscribe(innerSubscriber);
112112
}
113-
return subscribeTo(result)(innerSubscriber) as Subscription;
113+
let subscription: Subscription;
114+
try {
115+
subscription = subscribeTo(result)(innerSubscriber) as Subscription;
116+
} catch (error) {
117+
innerSubscriber.error(error);
118+
}
119+
return subscription;
114120
}

0 commit comments

Comments
 (0)