Challenge - 5 Problems
Exception Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when an exception is thrown inside the interceptor?
Consider a NestJS interceptor that catches exceptions and maps them to a custom error response. What will the client receive if the intercepted method throws a standard Error with message 'Failed operation'?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, catchError, throwError } from 'rxjs'; @Injectable() export class ExceptionMappingInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( catchError(err => { return throwError(() => new Error('Mapped: ' + err.message)); }), ); } } // Controller method throws new Error('Failed operation')
Attempts:
2 left
💡 Hint
Think about how catchError modifies the error before rethrowing it.
✗ Incorrect
The interceptor catches the original error and throws a new Error with the message prefixed by 'Mapped: '. This means the client will see the modified error message.
📝 Syntax
intermediate2:00remaining
Which option correctly implements an Exception Mapping Interceptor in NestJS?
Select the code snippet that correctly implements an interceptor that catches exceptions and maps them to a custom error message.
Attempts:
2 left
💡 Hint
Remember that catchError must return an Observable, and throwError is used to rethrow errors in RxJS.
✗ Incorrect
Option B correctly uses catchError with throwError(() => new Error(...)) which returns an Observable that emits an error. Other options either misuse try/catch with Observables, return an Error object instead of an Observable, or call throwError incorrectly.
🔧 Debug
advanced2:00remaining
Why does this Exception Mapping Interceptor fail to catch errors?
Given the following interceptor code, why does it not catch exceptions thrown by the controller method?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable } from 'rxjs'; @Injectable() export class FaultyInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { try { return next.handle(); } catch (err) { throw new Error('Intercepted error: ' + err.message); } } }
Attempts:
2 left
💡 Hint
Think about how Observables handle errors asynchronously.
✗ Incorrect
The try/catch block only catches synchronous errors. Since next.handle() returns an Observable, errors inside it happen asynchronously and must be caught with catchError operator.
❓ state_output
advanced2:00remaining
What is the final error message sent to the client?
An Exception Mapping Interceptor modifies errors by appending ' - intercepted' to the message. The controller throws new Error('Database failure'). What error message does the client receive?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, catchError, throwError } from 'rxjs'; @Injectable() export class AppendMessageInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( catchError(err => { err.message += ' - intercepted'; return throwError(() => err); }), ); } } // Controller throws new Error('Database failure')
Attempts:
2 left
💡 Hint
The interceptor modifies the original error message before rethrowing it.
✗ Incorrect
The interceptor appends ' - intercepted' to the original error message, so the client sees the combined message.
🧠 Conceptual
expert2:00remaining
Which statement about Exception Mapping Interceptors in NestJS is TRUE?
Select the correct statement about how Exception Mapping Interceptors work in NestJS.
Attempts:
2 left
💡 Hint
Think about the scope and purpose of interceptors versus filters.
✗ Incorrect
Exception Mapping Interceptors catch errors in the Observable stream of a request and can transform them. They must be applied explicitly and do not replace global exception filters. They are not class decorators.