0
0
NestJSframework~20 mins

Timeout interceptor in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timeout Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a NestJS TimeoutInterceptor times out?
Consider a NestJS TimeoutInterceptor that throws a timeout error if the request takes longer than 3 seconds. What will the client receive if the handler takes 5 seconds to respond?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, timeout, catchError } from 'rxjs';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(3000),
      catchError(err => {
        if (err.name === 'TimeoutError') {
          return throwError(() => new RequestTimeoutException());
        }
        return throwError(() => err);
      }),
    );
  }
}
AThe client receives an empty response with status 200.
BThe client receives a 408 Request Timeout error.
CThe client waits indefinitely until the handler finishes.
DThe client receives a 500 Internal Server Error.
Attempts:
2 left
💡 Hint
Think about what the RequestTimeoutException does in NestJS.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this TimeoutInterceptor code
Which option contains a syntax error that prevents the TimeoutInterceptor from compiling?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, timeout } from 'rxjs';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(5000)
    );
  }
}
AThe intercept method is missing the return type Observable<any>.
BMissing import of catchError from 'rxjs' causes runtime error.
Ctimeout operator is used without parentheses: timeout instead of timeout().
DThe class is missing the @Injectable decorator.
Attempts:
2 left
💡 Hint
Check how the timeout operator is called in RxJS.
🔧 Debug
advanced
2:30remaining
Why does the TimeoutInterceptor not trigger on slow requests?
A developer wrote this TimeoutInterceptor but it never triggers even when requests take longer than 10 seconds. What is the likely cause?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, timeout, catchError } from 'rxjs';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    next.handle().pipe(
      timeout(10000),
      catchError(err => {
        if (err.name === 'TimeoutError') {
          return throwError(() => new RequestTimeoutException());
        }
        return throwError(() => err);
      }),
    );
    return next.handle();
  }
}
AThe interceptor returns next.handle() without the pipe, so timeout is never applied.
BThe timeout duration is too long to trigger in tests.
CThe catchError operator is missing a return statement.
DThe interceptor is missing the @Injectable decorator.
Attempts:
2 left
💡 Hint
Check what the intercept method returns.
state_output
advanced
2:00remaining
What is the output when TimeoutInterceptor wraps a fast handler?
Given this TimeoutInterceptor with 2 seconds timeout, and a handler that responds in 1 second, what will the client receive?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, timeout, catchError } from 'rxjs';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(2000),
      catchError(err => {
        throw err;
      }),
    );
  }
}

// Handler code:
// async function handle() { await new Promise(r => setTimeout(r, 1000)); return { message: 'done' }; }
AThe client receives a 500 Internal Server Error.
BThe client receives a 408 Request Timeout error.
CThe client receives an empty response immediately.
DThe client receives { message: 'done' } after 1 second.
Attempts:
2 left
💡 Hint
The handler finishes before the timeout limit.
🧠 Conceptual
expert
3:00remaining
How does NestJS TimeoutInterceptor integrate with RxJS to handle slow requests?
Which statement best describes how a NestJS TimeoutInterceptor uses RxJS operators to manage request timeouts?
AIt uses the RxJS timeout operator to emit an error if the observable does not emit within the specified time, which is caught and transformed into a NestJS exception.
BIt uses the RxJS delay operator to postpone the response and cancels the request if it takes too long.
CIt uses the RxJS map operator to transform the response into a timeout error if it takes longer than the limit.
DIt uses the RxJS retry operator to repeat the request until it finishes within the timeout period.
Attempts:
2 left
💡 Hint
Think about how timeout errors are generated and handled in RxJS.