Helder Norte
Helder Norte

Reputation: 37

RxJs7 deprecated throwError

I've been following the NestJS course and during the section about Interceptors we need to create a TimeoutInterceptor as so:

export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(3000),
      catchError(err => {
        if (err instanceof TimeoutError) {
          return throwError(new RequestTimeoutException());
        }
        return throwError(err);
      }),
    );
  }
}

But my VSCode tells me throwError has been deprecated in RxJs v7, so I've come with the following solution:

export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(3000),
      catchError((err) => {
        if (err instanceof TimeoutError) {
          throw new RequestTimeoutException();
        }

        throw new Error(err);
      }),
    );
  }
}

I haven't found anything on the NestJS documentation that seems up to date and the method still seems to work even if it's deprecated, so I was wondering if my solution is viable and safe for production environments or if should I stick to the official NestJS documentation? Or maybe there's another better solution that I just haven't found yet online...

Thanks in advance for taking the time of answering me.

Upvotes: 0

Views: 2956

Answers (1)

Naren Murali
Naren Murali

Reputation: 56763

You need to change it to.

    export class TimeoutInterceptor implements NestInterceptor {
      intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        return next.handle().pipe(
          timeout(3000),
          catchError(err => {
            if (err instanceof TimeoutError) {
              return throwError(() => new RequestTimeoutException());
            }
            return throwError(() => err);
          }),
        );
      }
    }

Upvotes: 4

Related Questions