Complete the code to import the necessary decorator for creating an interceptor.
import { [1] } from '@nestjs/common';
The @Injectable decorator is required to define an interceptor class in NestJS.
Complete the code to implement the intercept method signature in the interceptor class.
intercept(context: ExecutionContext, next: [1]): Observable<any> {The intercept method receives a CallHandler as the second argument to handle the request flow.
Fix the error in the pipe method to catch exceptions and map them.
return next.handle().pipe(catchError(error => throwError(() => new [1](error.message, 500))));
To map exceptions properly, you throw a new HttpException with the error message.
Fill both blanks to create a custom exception mapping interceptor class.
import { [1], NestInterceptor, ExecutionContext, CallHandler, HttpException } from '@nestjs/common'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; @[2]() export class ExceptionMappingInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( catchError(error => { return throwError(() => new HttpException(error.message, error.status || 500)); }), ); } }
The class must be decorated with @Injectable and import it to be recognized as an interceptor.
Fill all three blanks to complete the interceptor that maps exceptions with status and message.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, HttpException } from '@nestjs/common'; import { Observable, throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; @Injectable() export class ExceptionMappingInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { return next.handle().pipe( catchError(error => { const status = error?.[1] || 500; const message = error?.[2] || 'Internal Server Error'; return throwError(() => new [3](message, status)); }), ); } }
The interceptor extracts status and message from the error object and throws a new HttpException with those values.