Challenge - 5 Problems
Timeout Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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); }), ); } }
Attempts:
2 left
💡 Hint
Think about what the RequestTimeoutException does in NestJS.
✗ Incorrect
The TimeoutInterceptor uses RxJS timeout operator to throw a TimeoutError if the handler takes longer than 3 seconds. This error is caught and transformed into a RequestTimeoutException, which NestJS translates into a 408 HTTP response.
📝 Syntax
intermediate2: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) ); } }
Attempts:
2 left
💡 Hint
Check how the timeout operator is called in RxJS.
✗ Incorrect
The timeout operator must be called as a function with parentheses, e.g., timeout(5000). Using timeout without parentheses is a syntax error.
🔧 Debug
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Check what the intercept method returns.
✗ Incorrect
The intercept method calls next.handle().pipe(...) but does not return it. Instead, it returns next.handle() again without the pipe, so the timeout operator is never applied.
❓ state_output
advanced2: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' }; }
Attempts:
2 left
💡 Hint
The handler finishes before the timeout limit.
✗ Incorrect
Since the handler responds within 1 second, which is less than the 2 seconds timeout, the TimeoutInterceptor lets the response pass through unchanged.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how timeout errors are generated and handled in RxJS.
✗ Incorrect
The TimeoutInterceptor applies the RxJS timeout operator to the observable returned by the handler. If the observable does not emit a value within the specified time, timeout emits an error. This error is caught and converted into a NestJS RequestTimeoutException.