0
0
NestJSframework~20 mins

Exception mapping interceptor in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
AThe client receives a generic error message 'Internal server error'.
BThe client receives the original error message 'Failed operation' without changes.
CThe client receives an error response with message 'Mapped: Failed operation'.
DThe interceptor swallows the error and the client receives a successful empty response.
Attempts:
2 left
💡 Hint
Think about how catchError modifies the error before rethrowing it.
📝 Syntax
intermediate
2: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.
A
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class MapExceptionInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    try {
      return next.handle();
    } catch (err) {
      throw new Error('Custom error: ' + err.message);
    }
  }
}
B
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, catchError, throwError } from 'rxjs';

@Injectable()
export class MapExceptionInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    return next.handle().pipe(
      catchError(err =&gt; throwError(() =&gt; new Error('Custom error: ' + err.message)))
    );
  }
}
C
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, catchError } from 'rxjs';

@Injectable()
export class MapExceptionInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    return next.handle().pipe(
      catchError(err =&gt; {
        return throwError(() =&gt; new Error('Custom error: ' + err.message));
      })
    );
  }
}
D
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';

@Injectable()
export class MapExceptionInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    return next.handle().pipe(
      catchError(err =&gt; throwError(() =&gt; new Error('Custom error: ' + err.message)))
    );
  }
}
Attempts:
2 left
💡 Hint
Remember that catchError must return an Observable, and throwError is used to rethrow errors in RxJS.
🔧 Debug
advanced
2: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);
    }
  }
}
ABecause next.handle() returns an Observable, errors inside it are asynchronous and must be caught using RxJS operators, not try/catch.
BBecause the interceptor is missing the @Catch decorator to catch exceptions.
CBecause the interceptor must return a Promise instead of an Observable to catch errors.
DBecause the try/catch block is placed after the next.handle() call, so it never executes.
Attempts:
2 left
💡 Hint
Think about how Observables handle errors asynchronously.
state_output
advanced
2: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')
AThe client receives an error with message 'Database failure'.
BThe client receives a generic error message 'Internal server error'.
CThe client receives an error with message ' - intercepted'.
DThe client receives an error with message 'Database failure - intercepted'.
Attempts:
2 left
💡 Hint
The interceptor modifies the original error message before rethrowing it.
🧠 Conceptual
expert
2:00remaining
Which statement about Exception Mapping Interceptors in NestJS is TRUE?
Select the correct statement about how Exception Mapping Interceptors work in NestJS.
AThey can transform exceptions thrown by route handlers into custom error responses by catching errors in the Observable stream.
BThey must be implemented as class decorators on controllers to work properly.
CThey replace the need for global exception filters by handling all error mapping internally.
DThey automatically catch all exceptions thrown anywhere in the application without needing to be applied.
Attempts:
2 left
💡 Hint
Think about the scope and purpose of interceptors versus filters.