Challenge - 5 Problems
Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this NestJS interceptor do to the response?
Consider this NestJS interceptor code. What will be the output behavior when applied to a controller method that returns a string?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, map } from 'rxjs'; @Injectable() export class AppendInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( map(data => data + '!!!') ); } }
Attempts:
2 left
💡 Hint
Look at the map operator inside the pipe and what it does to the data.
✗ Incorrect
The interceptor uses RxJS map to modify the response data by appending '!!!' to it before sending it back.
❓ lifecycle
intermediate1:30remaining
When is the intercept() method called in a NestJS interceptor?
In the lifecycle of a NestJS request, at what point does the intercept() method of an interceptor run?
Attempts:
2 left
💡 Hint
Interceptors wrap around the route handler execution.
✗ Incorrect
The intercept() method runs before the route handler and can modify the request or the response stream by wrapping the handler's observable.
📝 Syntax
advanced2:30remaining
Which option correctly implements a NestJS interceptor that logs the time taken by a request?
Choose the correct interceptor code that logs the time taken to process a request.
Attempts:
2 left
💡 Hint
Use tap to perform side effects without changing the data stream.
✗ Incorrect
Option A correctly uses tap to log the time after the observable completes without modifying the data. Option A subscribes twice causing duplicate calls. Option A uses map which changes the data to undefined. Option A adds an artificial delay which is not required.
🔧 Debug
advanced2:00remaining
Why does this interceptor cause a runtime error?
Identify the cause of the runtime error in this interceptor code.
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, map } from 'rxjs'; @Injectable() export class ErrorInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( map(data => data.toUpperCase()) ); } }
Attempts:
2 left
💡 Hint
Check the type of data before calling string methods.
✗ Incorrect
If the response data is not a string, calling toUpperCase() will cause a runtime error because that method does not exist on other types.
🧠 Conceptual
expert1:30remaining
What is the main purpose of the NestJS Interceptor interface?
Choose the best description of what the Interceptor interface allows you to do in NestJS.
Attempts:
2 left
💡 Hint
Think about how interceptors wrap around the handler execution.
✗ Incorrect
Interceptors allow you to run code before and after the route handler, modifying requests or responses, adding logging, caching, or transforming data.