title: How to catch exception thrown from observable subscription in test
body: <p>I'm not sure how to handle exceptions with URL parameters in Angular 6. Right now my service throws an <code>Error</code> when the URL id cannot be found. In the actual app I like that the error bubbles up to be caught and logged, but in my jasmine tests that results in a test failure:</p>
  
  <pre><code>HeroDetailComponent should navigate to not found page
  [object ErrorEvent] thrown
  </code></pre>
  
  <p>I've tried various <code>try {} catch () {}</code> blocks and <code>catchError</code> pipelines to handle the error in jasmine, but nothing seems to be able to catch this Error after the test expects run.</p>
  
  <p>Demo of the issue: <a href="https://angular-observable-catch.stackblitz.io/" rel="nofollow noreferrer">https://angular-observable-catch.stackblitz.io/</a></p>
  
  <p>Note that on stackblitz the test doesn't fail, but it does locally when run in my application using <code>ng test</code>.</p>
  
  <p>In the (main) console note the error:</p>
  
  <pre><code>Uncaught Error: Hero 999 not found.
      at HeroService.getHeroById (hero.service.ts:33)
      at SwitchMapSubscriber.eval [as project] (hero-detail.component.ts:46)
      at SwitchMapSubscriber._next (switchMap.ts:103)
      at SwitchMapSubscriber.Subscriber.next (Subscriber.ts:104)
      at ReplaySubject.Subject.next (Subject.ts:62)
      at ReplaySubject.nextInfiniteTimeWindow (ReplaySubject.ts:42)
      at ActivatedRouteStub.setParamMap (activated-route-stub.ts:56)
      at UserContext.eval (hero-detail.component.spec.ts:65)
      at ZoneDelegate.invoke (zone.js:388)
      at ProxyZoneSpec.onInvoke (zone-testing.js:288)
  </code></pre>
  
  <p>Source code for the demo: <a href="https://stackblitz.com/edit/angular-observable-catch?file=src%2Fapp%2Fhero%2Fhero-detail.component.spec.ts" rel="nofollow noreferrer">https://stackblitz.com/edit/angular-observable-catch?file=src%2Fapp%2Fhero%2Fhero-detail.component.spec.ts</a></p>
  
  <p>How do I catch this error in my jasmine tests so it doesn't cause an Uncaught Error?</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>I figured out that this is caused by the <a href="https://github.com/angular/angular/blob/8282e15c2becbe42a49befa07d6407247e8243d8/packages/common/src/pipes/async_pipe.ts#L22" rel="nofollow noreferrer">AsyncPipe subscription</a> which throws any errors the Observable/Promise/etc.</p>