0
0
NestJSframework~10 mins

Exception mapping interceptor in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the necessary decorator for creating an interceptor.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
AInjectable
BController
CModule
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller instead of @Injectable
Forgetting to import any decorator
Using @Module or @Service which are unrelated here
2fill in blank
medium

Complete the code to implement the intercept method signature in the interceptor class.

NestJS
intercept(context: ExecutionContext, next: [1]): Observable<any> {
Drag options to blanks, or click blank then click option'
ACallHandler
BRequest
CResponse
DHttpException
Attempts:
3 left
💡 Hint
Common Mistakes
Using Request or Response instead of CallHandler
Confusing with exception classes
3fill in blank
hard

Fix the error in the pipe method to catch exceptions and map them.

NestJS
return next.handle().pipe(catchError(error => throwError(() => new [1](error.message, 500))));
Drag options to blanks, or click blank then click option'
AObservable
BError
CHttpException
DInterceptor
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing generic Error instead of HttpException
Using Observable or Interceptor which are not error classes
4fill in blank
hard

Fill both blanks to create a custom exception mapping interceptor class.

NestJS
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));
      }),
    );
  }
}
Drag options to blanks, or click blank then click option'
AInjectable
BController
CModule
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Controller or @Module decorators
Forgetting to import Injectable
5fill in blank
hard

Fill all three blanks to complete the interceptor that maps exceptions with status and message.

NestJS
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));
      }),
    );
  }
}
Drag options to blanks, or click blank then click option'
Astatus
Bmessage
CHttpException
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names like 'error' instead of 'status' or 'message'
Throwing generic Error instead of HttpException