title: Interceptor not catching error thrown by guard in nestjs
body: <p>I have a global guard that is registered in the application in <code>common.module.ts</code> which is a global module.</p>
  <pre class="lang-js prettyprint-override"><code>    const HeaderGuardGlobal = {
          provide: APP_GUARD,
          useClass: HeaderGuard
      };
  
      @Global()
      @Module({
          imports: [ LoggerModule ],
          providers: [ HeaderGuardGlobal ],
          exports: []
      })
  </code></pre>
  <p>header.guard.ts:</p>
  <pre class="lang-js prettyprint-override"><code>    async canActivate(context: ExecutionContext): Promise&lt;boolean&gt; {
          const request = context.switchToHttp().getRequest();
          const userName = request.headers[HEADERS.USER_NAME];
  
          if(!userName) {
              throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
          }
  </code></pre>
  <p>I have a control-scoped interceptor <code>authenticate-header.interceptor.ts</code></p>
  <pre class="lang-js prettyprint-override"><code>    @Injectable()
      export class setAuthenticateHeaderInterceptor&lt;T&gt; implements NestInterceptor&lt;T, Response&lt;T&gt;&gt; {
          public constructor() {}
  
          intercept(context: ExecutionContext, next: CallHandler): Observable&lt;Response&lt;T&gt;&gt; {
              const req = context.switchToHttp().getRequest();
              const res = context.switchToHttp().getResponse();
              
              return next
              .handle()
              .pipe(
                  catchError(err =&gt; {
                      console.log('ERROR: ', err);
                          res.setHeader('sampleKey', 'sampleValue');
                          return throwError(err);
                      })
                  )
          }
      }
  </code></pre>
  <p>user.controller.ts:</p>
  <pre class="lang-js prettyprint-override"><code>    @Controller('user')
      @UseInterceptors(setAuthenticateHeaderInterceptor)
      export class ClientController {
  </code></pre>
  <p>What I'm trying to achieve is that when <code>header.guard.ts</code> throws <code>Forbidden</code> exception, the <code>authenticate-header.interceptor.ts</code> would catch the exception and will propagate it to the global http-filter, but before doing that I want to add a header to the response object.</p>
  <p>The issue I'm facing is when the exception is being thrown by the guard, the interceptor is unable to catch it. However, when the same error is thrown either from the route handler or from service, the interceptor is able to catch it.</p>
  <p>I went through the <a href="https://docs.nestjs.com/faq/request-lifecycle" rel="nofollow noreferrer">request-lifecycle</a> to understand the execution context, and found the below statement in <a href="https://docs.nestjs.com/faq/request-lifecycle#interceptors" rel="nofollow noreferrer">interceptors</a> section.</p>
  <blockquote>
  <p>any errors thrown by pipes, controllers, or services can be read in the catchError operator of an interceptor.</p>
  </blockquote>
  <p>The statement doesn't say anything about guards, so I'm assuming what I'm trying to achieve is not possible.</p>
  <p>I'm unable to figure out why an error thrown inside a guard is not being caught by the interceptor. The above code snippets only include the parts which I thought were necessary for the question, if anybody feels like more info is needed. then I'll provide it.</p>