title: NestJS interceptor - handle error inside .pipe()
body: <p>I'd like to implement a NestJS-interceptor that creates and writes an elasticSearch entry before the request handler is hit and that updates this entry with error/success-info after the handler has finished. To do this I am using:</p>
  <pre><code>@Injectable()
  export class ElasticsearchInterceptor implements NestInterceptor {
  
  constructor(private readonly elasticSearchService: ElasticsearchService) {}
  
  async intercept(_context: ExecutionContext, next: CallHandler): Promise &lt; Observable &lt; any &gt;&gt; {
      const elasticSearchPayload = new ElasticsearchPayloadBuilder()
          .setOperation(...)
          .build();
      const elasticSearchEntry = await this.elasticSearchService.writeEntry(elasticSearchPayload);
  
      return next
      .handle()
      .pipe(
          catchError(err =&gt; {
              elasticSearchPayload.status = err.status;
              return throwError(err);
          }),
          tap(() =&gt; {
              elasticSearchPayload.status = 'success';
          }),
          finalize(() =&gt; {
              this.elasticSearchService.updateEntry(elasticSearchEntry.id, elasticSearchPayload));
          }));
  }
  </code></pre>
  <p>As long as the <code>updateEntry</code>-call resolves, this works fine, but in case that it fails, this results in an unhandled rejection. I'd like to make sure that the error is caught and thrown. I tried converting the <code>updateEntry</code>-promise into a new Observable using</p>
  <pre><code>finalize(() =&gt; {
      return from(this.elasticSearchService.updateEntry(elasticSearchEntry.id, elasticSearchPayload))
      .pipe(
          catchError(err =&gt; throwError(err)));
  }));
  </code></pre>
  <p>but this does not solve the issue. How can I prevent the unhandled rejection and return the error from <code>updateEntry</code>?</p>